home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / mpatrol / doc / mpatrol.txt < prev    next >
Text File  |  2000-05-16  |  343KB  |  7,731 lines

  1. This file documents mpatrol, a library for controlling and tracing
  2. dynamic memory allocations.
  3.  
  4.    Copyright (C) 1997-2000 Graeme S. Roy <graeme@epc.co.uk>
  5.  
  6.    This is edition 1.9 of the mpatrol manual for version 1.2.0, 16th
  7. May, 2000.
  8.  
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.  
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided that
  15. the entire resulting derived work is distributed under the terms of a
  16. permission notice identical to this one.
  17.  
  18.    Permission is granted to copy and distribute translations of this
  19. manual into another language, under the above conditions for modified
  20. versions, except that this permission notice may be stated in a
  21. translation approved by the Free Software Foundation.
  22.  
  23.    All product names mentioned in the documentation and source code for
  24. this library are the trademarks of their respective owners.
  25.  
  26.  
  27. ...Table of Contents...
  28. mpatrol
  29. *******
  30.  
  31.                                  _                     _
  32.                                 | |                   | |
  33.  ____ ___    _____     _____   _| |_   _____   ____   | |
  34. |  _ ~ _ \  |  __ \   / __  | |_   _| |  ___| / __ \  | |
  35. | | | | | | | |  | | | |  | |   | |   | |    | |  | | | |
  36. | | | | | | | |__| | | |__| |   | |   | |    | |__| | | |
  37. |_| |_| |_| |  ___/   \_____|   |_|   |_|     \____/  |_|
  38.             | |
  39.             |_|
  40.  
  41.    This document describes mpatrol, a library for controlling and
  42. tracing dynamic memory allocations.
  43.  
  44.    This is edition 1.9 of the mpatrol manual for version 1.2.0, 16th
  45. May, 2000.
  46.  
  47. Foreword
  48. ********
  49.  
  50.    I first started writing this library a few years ago when the
  51. company I work for sent me out to a customer who had reported a memory
  52. leak, which he expected was coming from the code generated by our C++
  53. compiler.  A few years on and the library has changed dramatically from
  54. its first beginnings, but I thought I'd release it publicly in case
  55. anyone else found it useful.
  56.  
  57.    When writing the library, I placed more emphasis on the quantity and
  58. quality of information about allocated memory rather than the speed and
  59. efficiency of allocating the actual memory.  This means that the
  60. library will use dramatically more memory than normal dynamic memory
  61. allocation libraries and can slow down to a crawl depending on which
  62. options you use.  However, the end results are likely to be accurate
  63. and reliable, and in most cases the library will run quite happily at a
  64. sane speed.
  65.  
  66.    The mpatrol library is by no means the only library of its kind.
  67. Solaris 7 has no less that 6 different malloc libraries, and there are
  68. plenty available as freeware or as commercial products.  Try to keep in
  69. mind that mpatrol comes with absolutely no warranty and so if it
  70. doesn't work for you and you need a fast solution, try some of the
  71. other libraries or products available.  I have listed some of the most
  72. popular at the end of this manual (*note Related software::).
  73.  
  74.    This manual is arranged so that complete reference material on the
  75. mpatrol library can be found in the appendices, while introductory and
  76. background material can be found in the preceding chapters and
  77. sections.  For readers who wish to delve right in and use the library,
  78. the Installation (*note Installation::) and Examples (*note Examples::)
  79. chapters should be enough to get started in combination with the quick
  80. reference card.  Otherwise, this manual should be read from beginning
  81. to end in order to get the most out of the software it describes.
  82.  
  83.    Due to their very nature, problems with dynamic memory allocations
  84. are notoriously difficult to reproduce and debug, and this is likely to
  85. be the case if you find a bug in the mpatrol library as it might be
  86. extremely hard to reproduce on another system.  Details on how to
  87. report bugs are given elsewhere in this document (*note Notes::), but
  88. it would be very useful if you could try to provide as much information
  89. as possible when reporting a problem, and that includes having a look
  90. in the library source code to see if it's obvious what is wrong.
  91. However, please try to read the FAQ first in case your question or
  92. problem is covered there since it is usually updated every time I
  93. receive a question about mpatrol.
  94.  
  95.    The latest version of the mpatrol library and this manual can always
  96. be found at `http://www.cbmamiga.demon.co.uk/mpatrol/', and any
  97. correspondence relating to mpatrol (bug reports, enhancement requests,
  98. compliments, etc.) should be sent to <mpatrol@cbmamiga.demon.co.uk>.
  99. The mpatrol library is also registered at FreshMeat
  100. (`http://freshmeat.net/') so you can receive notification of updates
  101. there as well.  I normally only check my e-mail about once or twice a
  102. week, so don't expect an immediate response.  I can also be reached at
  103. <graeme@epc.co.uk> but that is my work e-mail address.  There is now
  104. also a discussion group at `http://www.egroups.com/group/mpatrol/'
  105. where you can post mpatrol-related questions but you must first
  106. subscribe to the group before you can send mail to it.
  107.  
  108.    Note that this manual is not just intended to instruct readers on
  109. how to use the mpatrol library -- it is also written to give a detailed
  110. look at how malloc libraries work in general and how to improve the
  111. efficiency of existing code which uses them.  If this subject interests
  112. you, you may find further useful material at _The Memory Management
  113. Reference_ located at `http://www.harlequin.com/mm/reference/'.  It has
  114. links to many documents and research papers in the field of memory
  115. management, and has a large glossary which lists and explains related
  116. terms.  You may also wish to look at _A Memory Allocator_ by Doug Lea
  117. for information on general memory allocation principles.  It is located
  118. at `http://gee.cs.oswego.edu/dl/html/malloc.html'.
  119.  
  120.    Finally, I'd like to thank Stephan Springl (<springl@bfw-online.de>)
  121. for his help on reading debugging information from object files via the
  122. GNU BFD library, and Dave Gibson (<david@epc.co.uk>) for his help on
  123. writing thread-safe code.  Calum Wilkie (<calum@epc.co.uk>) also
  124. deserves a mention since the idea for providing stack traces comes from
  125. a similar library he wrote a few years ago.
  126.  
  127.    Oh, and always remember to do final release builds without the
  128. mpatrol library as the library is much slower than normal malloc
  129. implementations and uses much more memory.
  130.  
  131.    Happy debugging!
  132.  
  133.    Graeme Roy, 11th October, 1999.
  134.  
  135.    Edinburgh, Scotland.
  136.  
  137. 1 Overview
  138. **********
  139.  
  140.    The mpatrol library is yet another link library that attempts to
  141. diagnose run-time errors that are caused by the wrong use of
  142. dynamically allocated memory.  If you don't know what the `malloc()'
  143. function or `operator new[]' do then this library is probably not for
  144. you.  You have to have a certain amount of programming expertise and a
  145. knowledge of how to run a command line compiler and linker before you
  146. should attempt to use this.
  147.  
  148.    Along with providing a comprehensive and configurable log of all
  149. dynamic memory operations that occurred during the lifetime of a
  150. program, the mpatrol library performs extensive checking to detect any
  151. misuse of dynamically allocated memory.  All of this functionality can
  152. be integrated into existing code through the inclusion of a single
  153. header file at compile-time.  On UNIX and Windows platforms (and
  154. AmigaOS when using `gcc') this may not even be necessary as the mpatrol
  155. library can be linked with existing object files at link-time or, on
  156. some platforms, even dynamically linked with existing programs at
  157. run-time.
  158.  
  159.    All logging and tracing output from the mpatrol library is sent to a
  160. separate log file in order to keep its diagnostics separate from any
  161. that the program being tested might generate.  A wide variety of
  162. library settings can also be changed at run-time via an environment
  163. variable, thus removing the need to recompile or relink in order to
  164. change the library's behaviour.
  165.  
  166.    A file containing a summary of the memory allocation profiling
  167. statistics for a particular program can be produced by the mpatrol
  168. library.  This file can then be read by a profiling tool which will
  169. display a set of tables based upon the accumulated data.  The profiling
  170. information includes summaries of all of the memory allocations listed
  171. by size and the function that allocated them and a list of memory leaks
  172. with the call stack of the allocating function.
  173.  
  174.    The mpatrol library has been designed with the intention of
  175. replacing calls to existing C and C++ memory allocation functions as
  176. seamlessly as possible, but in many cases that may not be possible and
  177. slight code modifications may be required.  However, a preprocessor
  178. macro containing the version of the mpatrol library is provided for the
  179. purposes of conditional compilation so that release builds and debug
  180. builds can be easily automated.
  181.  
  182. 2 Features
  183. **********
  184.  
  185.    An overall list of features contained in the mpatrol library is
  186. given below.  This is not intended to be exhaustive since the best way
  187. to see what the library does is to read the documentation and try it
  188. out.
  189.  
  190.    * Written for UNIX, AmigaOS, Windows and Netware platforms.
  191.  
  192.    * Can be built to allocate memory from a fixed-sized static array
  193.      rather than using heap memory from the system.
  194.  
  195.    * Can be built as archive, shared and/or thread-safe libraries on
  196.      systems that support them, or even as one large object file.  A
  197.      lint library can also be built from the mpatrol library on UNIX
  198.      platforms.
  199.  
  200.    * Details of memory allocations and free memory are stored
  201.      internally as a tree structure for speed and also to allow the
  202.      best fit allocation algorithm to be used.  This also enables the
  203.      library to perform intelligent resizing of memory allocations and
  204.      can be used to quickly determine if an address has been allocated
  205.      on the heap.
  206.  
  207.    * Contains 14 replacement C dynamic memory allocation functions:
  208.  
  209.      `malloc()'    ANSI    Allocates memory.
  210.      `calloc()'    ANSI    Allocates zero-filled memory.
  211.      `memalign()'  UNIX    Allocates memory with a specified alignment.
  212.      `valloc()'    UNIX    Allocates page-aligned memory.
  213.      `pvalloc()'   UNIX    Allocates a number of pages.
  214.      `strdup()'    UNIX    Duplicates a string.
  215.      `strndup()'   old     Duplicates a string with a maximum length.
  216.      `strsave()'   old     Duplicates a string.
  217.      `strnsave()'  old     Duplicates a string with a maximum length.
  218.      `realloc()'   ANSI    Resizes memory.
  219.      `recalloc()'  old     Resizes memory allocated by `calloc()'.
  220.      `expand()'    old     Resizes memory but does not relocate it.
  221.      `free()'      ANSI    Frees memory.
  222.      `cfree()'     old     Frees memory allocated by `calloc()'.
  223.  
  224.    * Contains 4 replacement C++ dynamic memory allocation functions:
  225.  
  226.      `operator new'       Allocates memory.
  227.      `operator new[]'     Allocates memory for an array.
  228.      `operator delete'    Frees memory.
  229.      `operator delete[]'  Frees memory allocated by `operator new[]'.
  230.  
  231.    * Contains 10 replacement C memory operation functions:
  232.  
  233.      `memset()'    ANSI    Fills memory with a specific byte.
  234.      `bzero()'     UNIX    Fills memory with the zero byte.
  235.      `memccpy()'   UNIX    Copies memory up to a specific byte.
  236.      `memcpy()'    ANSI    Copies non-overlapping memory.
  237.      `memmove()'   ANSI    Copies possibly-overlapping memory.
  238.      `bcopy()'     UNIX    Copies possibly-overlapping memory.
  239.      `memcmp()'    ANSI    Compares two blocks of memory.
  240.      `bcmp()'      UNIX    Compares two blocks of memory.
  241.      `memchr()'    ANSI    Searches memory for a specific byte.
  242.      `memmem()'    UNIX    Searches memory for specific bytes.
  243.  
  244.    * All of the above functions can also be defined with an additional
  245.      underscore prepended to their external name in order to catch all
  246.      uses of these functions in the system and third-party libraries.
  247.  
  248.    * Contains support for a user-defined low-memory handler function,
  249.      including a replacement for the C++ function, `set_new_handler()'.
  250.  
  251.    * Contains support for user-defined prologue and epilogue callback
  252.      functions, which get called before and after every memory
  253.      allocation, reallocation or deallocation.
  254.  
  255.    * A function is provided to return as much information as possible
  256.      about a given memory allocation, and can be called at any time
  257.      during program execution.  A similar function is also provided for
  258.      calling from within a debugger and an example command file is
  259.      provided for use with `gdb'.
  260.  
  261.    * A function is provided to display library settings and heap usage
  262.      statistics, including peak memory usage.  This information is also
  263.      displayed at program termination.
  264.  
  265.    * The library reads any user-controllable options at run-time from
  266.      an environment variable, but this does not have to be set as
  267.      defaults will then be used.  This prevents having to recompile
  268.      anything in order to change any library settings.  An option
  269.      exists to display a quick-reference summary of all of the
  270.      recognised options to the standard error file stream.
  271.  
  272.    * All diagnostics and logging are sent to a file in the current
  273.      directory, but this can be overridden, including forcing the log
  274.      file to be the standard output or standard error file streams.
  275.  
  276.    * Options exist to log details of every memory allocation,
  277.      reallocation or deallocation when they occur.
  278.  
  279.    * Options exist to halt the program at a specific memory allocation,
  280.      reallocation or deallocation when running the program within a
  281.      debugger.  These options have no effect when running the program
  282.      without a debugger.
  283.  
  284.    * An option exists to enable memory allocation profiling, which
  285.      forces a summary of all memory allocation statistics to be written
  286.      to a specified file for later use by a profiling command.  The
  287.      profiling file can also be written at a specified frequency.
  288.  
  289.    * A profiling command is provided which reads a profiling output
  290.      file produced by the mpatrol library and displays a set of tables
  291.      based on the accumulated data.  The profiling information includes
  292.      summaries of all of the memory allocations listed by size and the
  293.      function that allocated them and a list of memory leaks with the
  294.      call stack of the allocating function.
  295.  
  296.    * On UNIX platforms, the `mmap()' function can optionally be used to
  297.      allocate memory instead of the `sbrk()' function, but only if the
  298.      system supports it.  This can be useful if the mpatrol library
  299.      clashes with another malloc library that uses `sbrk()' to allocate
  300.      heap memory.
  301.  
  302.    * On non-UNIX platforms where the mpatrol library overrides
  303.      `malloc()' without requiring the inclusion of `mpatrol.h',
  304.      versions of the UNIX functions `brk()' and `sbrk()' are provided
  305.      for compatibility with certain libraries.  These should _not_ be
  306.      called by user code as they have only limited functionality.
  307.  
  308.    * All newly-allocated memory that is not allocated by the `calloc()'
  309.      or `recalloc()' functions will be pre-filled with a non-zero value
  310.      in order to catch out programs that wrongly assume that all
  311.      newly-allocated memory is zeroed.  This value can be modified at
  312.      run-time.
  313.  
  314.    * Can automatically check to see if there have been any illegal
  315.      writes to bytes located just before and after every memory
  316.      allocation through the use of overflow buffers.  The size of such
  317.      overflow buffers and the value to pre-fill them with can be
  318.      modified at run-time.  The checks will be performed before every
  319.      memory allocation call to ensure that nothing has overwritten the
  320.      overflow buffers, but a function is also provided to perform
  321.      additional checks under the programmer's control and an option
  322.      exists to specify a range in which checks will be performed.
  323.  
  324.    * On systems that support them, watch point areas can be used
  325.      instead of overflow buffers so that every read and write to memory
  326.      is checked to ensure that it is not within an overflow buffer.
  327.  
  328.    * Can automatically check to see if there have been any illegal
  329.      writes to free memory blocks.  The value to pre-fill free memory
  330.      blocks with can be modified at run-time.  The check will be
  331.      performed before every memory allocation call to ensure that
  332.      nothing has overwritten the free memory block, but a function is
  333.      also provided to perform additional checks under the programmer's
  334.      control and an option exists to specify a range in which checks
  335.      will be performed.
  336.  
  337.    * On systems that support memory protection, every memory allocation
  338.      can optionally be allocated at least one page of memory.  That
  339.      way, any free memory blocks can be made read and write protected
  340.      so that nothing can access free memory on the heap.  An option is
  341.      provided to specify whether all memory allocations should be
  342.      allocated at the start or at the end of such pages, and the bytes
  343.      left over within the pages become overflow buffers.
  344.  
  345.    * All freed memory allocations can optionally be prevented from
  346.      being returned to the free memory pool.  This is useful for
  347.      detecting if use is being made of freed memory just after a memory
  348.      allocation has been freed.  The contents of the memory allocation
  349.      can either be preserved or can be pre-filled with a value in order
  350.      to detect illegal writes to the freed memory allocation.
  351.  
  352.    * Calls to memory operation functions (such as `memset()' or
  353.      `memcpy()') have their arguments checked to ensure that they do
  354.      not pass null pointers or attempt to read or write memory
  355.      straddling the boundary of a previously allocated memory block,
  356.      although an option exists to turn such an error into a warning so
  357.      that the operation can still be performed.  Tracing from all such
  358.      functions can also optionally be written to the log file.
  359.  
  360.    * The internal data structures used by the library are kept separate
  361.      from the rest of the memory allocations.  On systems that support
  362.      memory protection, all of these internal data structures will be
  363.      write-protected in order to prevent corruption by the calling
  364.      program.  This feature can be overridden at run-time as it can
  365.      slow the program down.
  366.  
  367.    * Certain signals can be saved and restored on entry to each library
  368.      function and `errno' is set to `ENOMEM' if memory cannot be
  369.      allocated.
  370.  
  371.    * On systems that support memory protection, the library attempts to
  372.      detect any illegal memory accesses and display as much information
  373.      as it can obtain about the address in question and where the
  374.      illegal memory access occurred.
  375.  
  376.    * A call stack traceback from any function performing a memory
  377.      allocation is stored if the library supports this feature on the
  378.      system it is being run on.  This information can then be displayed
  379.      when information about a specific memory allocation is required.
  380.      Two different call stack traceback implementations are provided.
  381.  
  382.    * Symbol table details from executable files and shared libraries are
  383.      automatically read on systems that support this feature in order
  384.      to make the call stack tracebacks more meaningful.  An option also
  385.      exists to display a complete list of the symbols that were read by
  386.      the library at program termination.
  387.  
  388.    * Compiler-generated line number tables from any debugging sections
  389.      that exist in executable files and shared libraries can also be
  390.      used by the mpatrol library in order to provide more meaningful
  391.      information in call stack tracebacks.
  392.  
  393.    * If the library is unable to automatically determine a program's
  394.      executable filename to read symbols from then an option exists to
  395.      specify the full path to the program's executable file.
  396.  
  397.    * An option exists to change the default alignment used for
  398.      general-purpose memory allocations.
  399.  
  400.    * Contains support for a user-defined limit to available memory
  401.      which can be useful for stress-testing a program in simulated low
  402.      memory conditions.
  403.  
  404.    * Contains a feature to randomly fail a specific frequency of memory
  405.      allocations which can be useful for stress-testing error recovery
  406.      code in a program.
  407.  
  408.    * An option exists to display a complete memory map of the heap at
  409.      program termination.  A function to do this is also available to
  410.      call at any point during program execution.
  411.  
  412.    * Options exist to display all freed and unfreed memory allocations
  413.      at program termination in order to detect memory leaks.  A
  414.      separate program is also provided for locating memory leaks in
  415.      unfinished log files.
  416.  
  417.    * An option exists to abort the program with a failure condition if
  418.      there are more than a specified number of unfreed memory
  419.      allocations at program termination.  This could be useful for
  420.      batch testing in order to check that all tests free up most of
  421.      their allocated memory.
  422.  
  423.    * Functions always report if their arguments are illegal in order to
  424.      pinpoint any errors, and options exist to perform rigorous
  425.      checking of arguments when allocating, reallocating and freeing
  426.      memory.  In addition, checking is performed to ensure that memory
  427.      allocated by `operator new[]' is not freed with `free()' for
  428.      example.
  429.  
  430.    * The type of function performing a memory allocation is always
  431.      stored along with the allocation, as well as the file and line
  432.      number it was called from.  If compiled with `gcc', the function
  433.      name will also be stored and the thread identifier will be stored
  434.      if using the thread-safe library.
  435.  
  436.    * The library uses a header file to redefine the memory allocation
  437.      functions as macros in order to obtain more information about
  438.      where they were called from.  This is not strictly required on
  439.      UNIX and Windows platforms (and AmigaOS when using `gcc'), since
  440.      the library automatically redefines the default system memory
  441.      allocation functions.  All redefinitions in the header can also be
  442.      disabled by defining the `NDEBUG' preprocessor macro.
  443.  
  444.    * A command is supplied to run a program that was linked with the
  445.      mpatrol library with any specified options on the command line.
  446.      On some UNIX platforms, an option also exists to override the
  447.      default memory allocation routines for any dynamically-linked
  448.      program that was not previously linked with the mpatrol library.
  449.  
  450.    * The mpatrol library can be built to liaise with Parasoft Inuse, a
  451.      commercial graphical memory usage tool that can display the
  452.      current memory map of a running process.  Inuse is supplied with
  453.      Parasoft Insure++.
  454.  
  455.    * A small test suite is provided in order to test basic features.
  456.  
  457.    * User documentation is currently available in TeXinfo format as
  458.      well as UNIX manual pages and a quick reference card.
  459.  
  460. 3 Installation
  461. **************
  462.  
  463.    The mpatrol library was initially developed on an Amiga 4000/040
  464. running AmigaOS 3.1.  I then installed RedHat Linux 5.1 on my Amiga and
  465. added support for Linux/m68k.  I've tried my best to make it as easy as
  466. possible to build and install mpatrol on any system, but it isn't
  467. likely to run smoothly for everybody.  However, there shouldn't be any
  468. major problems if you perform the following steps.
  469.  
  470.   1. Go into the `build' directory and then into the appropriate
  471.      subdirectory for your system.
  472.  
  473.   2. Edit the `Makefile' in that directory and check that it is using
  474.      the appropriate compiler and build tools.  The `CC' macro
  475.      specifies the compiler, the `AR' macro specifies the tool used to
  476.      build the archive library and the `LD' macro specifies the tool to
  477.      build the shared library.  The `CFLAGS' macro specifies compiler
  478.      options that are always to be used, the `OFLAGS' macro specifies
  479.      optimisation options for the compiler, the `SFLAGS' macro
  480.      specifies options to be passed to the compiler when building a
  481.      shared library and the `TFLAGS' macro specifies options to be
  482.      passed to the compiler when building a thread-safe library.  You
  483.      may also need to change the library names and library build
  484.      commands on different systems.
  485.  
  486.   3. Use the `make' command (or equivalent) to build the mpatrol
  487.      library in archive form.  The `all' target builds all possible
  488.      combinations of the mpatrol library for your system.  The `clean'
  489.      target removes all relevant object files from the current
  490.      directory, while the `clobber' target also removes all libraries
  491.      that have been built from the current directory.  On some UNIX
  492.      platforms, the `lint' target will build a `lint' library for the
  493.      mpatrol library.
  494.  
  495.   4. If the mpatrol library is to be built with support for Parasoft
  496.      Inuse then the `MP_INUSE_SUPPORT' preprocessor macro must be
  497.      defined in the `CFLAGS' portion of the `Makefile' before building.
  498.      This will ensure that Inuse will be notified of every memory
  499.      allocation, reallocation and deallocation, but the Insure++
  500.      runtime library will also have to be linked in with any program
  501.      that uses mpatrol.
  502.  
  503.   5. Copy all of the libraries that have been built into your local
  504.      library directory.  If there were symbolic links created in the
  505.      `build' directory then these should be recreated in the local
  506.      library directory rather than simply copying them.
  507.  
  508.   6. Copy the `mpatrol', `mprof' and `mleak' programs that have been
  509.      built into your local bin directory.
  510.  
  511.   7. Go up two directory levels into the `src' directory and copy the
  512.      `mpatrol.h' header file into your local include directory.
  513.  
  514.   8. On UNIX platforms, go up one directory level into the `man'
  515.      directory and copy the `man1' and `man3' subdirectories to your
  516.      local man directory.  Unfortunately, the location for manual pages
  517.      varies from system to system so you may or may not also be able to
  518.      copy the `cat1' and `cat3' subdirectories as well.  The `man*'
  519.      subdirectories contain the unformatted manual pages while the
  520.      `cat*' subdirectories contain the formatted manual pages.
  521.  
  522.   9. Go up one directory level into the `doc' directory and examine the
  523.      files located there.  The `mpatrol.texi' file contains the TeXinfo
  524.      source for this manual and can be translated into a wide variety
  525.      of documentation formats.  The `refcard.tex' file contains the
  526.      LaTeX source for the quick reference card and can be translated
  527.      into formats suitable for printing onto a single page.  There may
  528.      already be translated files in the `doc' directory, but if not you
  529.      will either have to generate them yourself using an appropriate
  530.      tool or you could download an archive containing the latest
  531.      mpatrol manual and reference card in a variety of documentation
  532.      formats from the mpatrol home page.  You can then install or print
  533.      these documents.
  534.  
  535.    Alternatively, the `pkg' directory contains files that can be used to
  536. automatically generate a _package_ in a specific format suitable for
  537. installation on a system.  Two package formats (PKG and RPM) and two
  538. archive formats are currently supported (generic tape archive and LhA).
  539. The first package format is generally used on UNIX SVR4 systems, while
  540. the second was introduced by Red Hat for use in their Linux
  541. distributions.  The generic tape archive can be used as a distribution
  542. for UNIX systems where no package format is supported, but it does not
  543. contain information on how to install the files on the system once they
  544. have been extracted from the distribution.  The LhA format is roughly
  545. the same, but is intended for Amiga systems and is used for Aminet
  546. distributions.  You should really know what you are doing before you
  547. attempt to build a package, and you should also be aware that some of
  548. the package files may need to be modified before you begin.
  549.  
  550. 4 Integration
  551. *************
  552.  
  553.    The following steps should allow you to easily integrate the mpatrol
  554. library into an existing application, although some of them may not be
  555. available to do on many platforms.  They are listed in the order of
  556. number of changes required to modify existing code -- the last step
  557. will require a complete recompilation of all your code.
  558.  
  559.   1. This step is currently only available on IRIX, Linux and Solaris
  560.      platforms and on DG/UX 4.20MU07 or later platforms with the
  561.      `LD_PRELOAD' feature.
  562.  
  563.      If your program or application has been dynamically linked with
  564.      the system C library (`libc.so') or an alternative malloc shared
  565.      library then you can use the `-d' option to the `mpatrol' command
  566.      to override the default definitions of `malloc()', etc. at
  567.      run-time without having to relink your program.
  568.  
  569.      For example, if your program's executable file is called
  570.      `testprog' and it accepts an option specifying an input file, you
  571.      can force the system's dynamic linker to use mpatrol's versions of
  572.      `malloc()', etc. instead of the default versions by typing:
  573.  
  574.           mpatrol -d ./testprog -i file
  575.  
  576.      The resulting log file should be called `mpatrol.<procid>.log' by
  577.      default (where PROCID is the current process id), but if no such
  578.      file exists after running the `mpatrol' command then it will not
  579.      be possible to force the run-time linking of mpatrol functions to
  580.      your program and you will have to proceed to the next step.
  581.  
  582.   2. This step is currently only available on UNIX and Windows
  583.      platforms (and AmigaOS when using `gcc').
  584.  
  585.      You should be able to link in the mpatrol library when linking
  586.      your program without having to recompile any of your object files
  587.      or libraries, but this will only be worthwhile on systems where
  588.      stack tracebacks are supported, otherwise you should proceed to
  589.      the next step since there will not be enough information for you
  590.      to tell where the calls to dynamic memory allocation functions took
  591.      place.
  592.  
  593.      Information on how to link the mpatrol library to an application
  594.      is given at the start of the examples (*note Examples::), but you
  595.      should note that if your program does not directly call any of the
  596.      functions in the mpatrol library then it will not be linked in and
  597.      you will not see a log file being generated when you run it.  You
  598.      can force the linking of the mpatrol library by causing `malloc()'
  599.      to be undefined on the link line, usually through the use of the
  600.      `-u' linker option.
  601.  
  602.   3. All of the following steps will require you to recompile some or
  603.      all of your code so that your code calls dynamic memory allocation
  604.      functions from the mpatrol library rather than the system C
  605.      library.
  606.  
  607.      For this step, if you have a rough idea of where the function
  608.      calls lie that you would like to trace or test, you need only
  609.      recompile the relevant source files.  You should modify these
  610.      source files to include the `mpatrol.h' header file before any
  611.      calls to dynamic memory allocation or memory operation functions.
  612.  
  613.      However, you should take particular care to ensure that all calls
  614.      to memory allocation functions in the mpatrol library will be
  615.      matched by calls to memory reallocation or deallocation functions
  616.      in the mpatrol library, since if they are unmatched then the log
  617.      file will either fill up with errors complaining about trying to
  618.      free unknown allocations, or warnings about unfreed memory
  619.      allocations at the end of execution.
  620.  
  621.   4. This step requires you to recompile all of your source files to
  622.      include the `mpatrol.h' header file.  Obviously, this will take
  623.      the longest amount of time to integrate, but need not require you
  624.      to change any source files if the compiler you are using has a
  625.      command line option to include a specific header file before any
  626.      source files.
  627.  
  628.      For example, `gcc' comes with a `-include' option which has this
  629.      feature, so if you had to recompile a source file called `test.c'
  630.      then the following command would allow you to include `mpatrol.h'
  631.      without having to modify the source file:
  632.  
  633.           gcc -include /usr/local/include/mpatrol.h -c test.c
  634.  
  635.    In all cases, it will be desirable to compile your source files with
  636. compiler-generated debugging information since that may be able to be
  637. used by the `USEDEBUG' option.  In addition, more symbolic information
  638. will be available if the executable files have not had their symbol
  639. tables stripped from them, although mpatrol can also fall back to using
  640. the dynamic symbol table from dynamically linked executable files.
  641.  
  642. 5 Memory allocations
  643. ********************
  644.  
  645.    In the C and C++ programming languages there are generally three
  646. different types of memory allocation that can be used to hold the
  647. contents of variables.  Other programming languages such as Pascal,
  648. BASIC and FORTRAN also support some of these types of allocation,
  649. although their implementations may be slightly different.
  650.  
  651. 5.1 Static memory allocations
  652. =============================
  653.  
  654.    The first type of memory allocation is known as a _static memory
  655. allocation_, which corresponds to file scope variables and local static
  656. variables.  The addresses and sizes of these allocations are fixed at
  657. the time of compilation(1) and so they can be placed in a fixed-sized
  658. data area which then corresponds to a section within the final linked
  659. executable file.  Such memory allocations are called static because
  660. they do not vary in location or size during the lifetime of the program.
  661.  
  662.    There can be many types of data sections within an executable file;
  663. the three most common are normal data, BSS data and read-only data.
  664. BSS data contains variables and arrays which are to be initialised to
  665. zero at run-time and so is treated as a special case, since the actual
  666. contents of the section need not be stored in the executable file.
  667. Read-only data consists of constant variables and arrays whose contents
  668. are guaranteed not to change when a program is being run.  For example,
  669. on a typical SVR4 UNIX system the following variable definitions would
  670. result in them being placed in the following sections:
  671.  
  672.      int a;           /* BSS data */
  673.      int b = 1;       /* normal data */
  674.      const int c = 2; /* read-only data */
  675.  
  676.    In C the first example would be considered a _tentative_
  677. declaration, and if there was no subsequent definition of that variable
  678. in the current translation unit then it would become a _common_
  679. variable in the resulting object file.  When the object file gets
  680. linked with other object files, any common variables with the same name
  681. become one variable, or take their definition from a non-tentative
  682. definition of that variable.  In the former case, the variable is
  683. placed in the BSS section.  Note that C++ has no support for tentative
  684. declarations.
  685.  
  686.    As all static memory allocations have sizes and address offsets that
  687. are known at compile-time and are explicitly initialised, there is very
  688. little that can go wrong with them.  Data can be read or written past
  689. the end of such variables, but that is a common problem with all memory
  690. allocations and is generally easy to locate in that case.  On systems
  691. that separate read-only data from normal data, writing to a read-only
  692. variable can be quickly diagnosed at run-time.
  693.  
  694.    ---------- Footnotes ----------
  695.  
  696.    (1) Or more accurately, at link time.
  697.  
  698. 5.2 Stack memory allocations
  699. ============================
  700.  
  701.    The second type of memory allocation is known as a _stack memory
  702. allocation_, which corresponds to non-static local variables and
  703. call-by-value parameter variables.  The sizes of these allocations are
  704. fixed at the time of compilation but their addresses will vary
  705. depending on when the function which defines them is called.  Their
  706. contents are not immediately initialised, and must be explicitly
  707. initialised by the programmer upon entry to the function or when they
  708. become visible in scope.
  709.  
  710.    Such memory allocations are placed in a system memory area called the
  711. _stack_, which is allocated per process(1) and generally grows down in
  712. memory.  When a function is called, the state of the calling function
  713. must be preserved so that when the called function returns, the calling
  714. function can resume execution.  That state is stored on the stack,
  715. including all local variables and parameters.  The compiler generates
  716. code to increase the size of the stack upon entry to a function, and
  717. decrease the size of the stack upon exit from a function, as well as
  718. saving and restoring the values of registers.
  719.  
  720.    There are a few common problems using stack memory allocations, and
  721. most generally involve uninitialised variables, which a good compiler
  722. can usually diagnose at compile-time.  Some compilers also have options
  723. to initialise all local variables with a bit pattern so that
  724. uninitialised stack variables will cause program faults at run-time.
  725. As with static memory allocations, there can be problems with reading
  726. or writing past the end of stack variables, but as their sizes are
  727. fixed these can usually easily be located.
  728.  
  729.    ---------- Footnotes ----------
  730.  
  731.    (1) Or per thread on some systems.
  732.  
  733. 5.3 Dynamic memory allocations
  734. ==============================
  735.  
  736.    The last type of memory allocation is known as a _dynamic memory
  737. allocation_, which corresponds to memory allocated via `malloc()' or
  738. `operator new[]'.  The sizes, addresses and contents of such memory vary
  739. at run-time and so can cause a lot of problems when trying to diagnose
  740. a fault in a program.  These memory allocations are called dynamic
  741. memory allocations because their location and size can vary throughout
  742. the lifetime of a program.
  743.  
  744.    Such memory allocations are placed in a system memory area called the
  745. _heap_, which is allocated per process on some systems, but on others
  746. may be allocated directly from the system in scattered blocks.  Unlike
  747. memory allocated on the stack, memory allocated on the heap is not
  748. freed when a function or scope is exited and so must be explicitly
  749. freed by the programmer.  The pattern of allocations and deallocations
  750. is not guaranteed to be (and is not really expected to be) linear and
  751. so the functions that allocate memory from the heap must be able to
  752. efficiently reuse freed memory and resize existing allocated memory on
  753. request.  In some programming languages there is support for a _garbage
  754. collector_, which attempts to automatically free memory that has had
  755. all references to it removed, but this has traditionally not been very
  756. popular for programming languages such as C and C++, and has been more
  757. widely used in functional languages like ML(1).
  758.  
  759.    Because dynamic memory allocations are performed at run-time rather
  760. than compile-time, they are outwith the domain of the compiler and must
  761. be implemented in a run-time package, usually as a set of functions
  762. within a linker library.  Such a package manages the heap in such a way
  763. as to abstract its underlying structure from the programmer, providing
  764. a common interface to heap management on different systems.  However,
  765. this _malloc library_ must decide whether to implement a fast memory
  766. allocator, a space-conserving memory allocator, or a bit of both.  It
  767. must also try to keep its own internal tables to a minimum so as to
  768. conserve memory, but this means that it has very little capability to
  769. diagnose errors if any occur.
  770.  
  771.    In some compiler implementations there is a builtin function called
  772. `alloca()'.  This is a dynamic memory allocation function that allocates
  773. memory from the stack rather than the heap, and so the memory is
  774. automatically freed when the function that called it returns.  This is
  775. a non-standard feature that is not guaranteed to be present in a
  776. compiler, and indeed may not be possible to implement on some systems.
  777. However, some compilers now support variable length arrays which
  778. provide roughly the same functionality.
  779.  
  780.    As can be seen from the above paragraphs, dynamic memory allocations
  781. are the types of memory allocations that can cause the most problems in
  782. a program since almost nothing about them can be used by the compiler
  783. to give the programmer useful warnings about using uninitialised
  784. variables, using freed memory, running off the end of a
  785. dynamically-allocated array, etc.  It is these types of memory
  786. allocation problems that the mpatrol library loves to get its teeth
  787. into!
  788.  
  789.    ---------- Footnotes ----------
  790.  
  791.    (1) There is currently at least one garbage collection package
  792. available for C and C++ (*note Related software::).
  793.  
  794. 6 Operating system support
  795. **************************
  796.  
  797.    Beneath every malloc library's public interface there is the
  798. underlying operating system's memory management interface.  This
  799. provides features which can be as simple as giving processes the
  800. ability to allocate a new block of memory for themselves, or it can
  801. offer advanced features such as protecting areas of memory from being
  802. read or written.  Some embedded systems have no operating systems and
  803. hence no support for dynamic memory allocation, and so the malloc
  804. library must instead allocate blocks of memory from a fixed-sized array.
  805. The mpatrol library can be built to support all of the above types of
  806. system, but the more features an operating system can provide it with,
  807. the more it can do.
  808.  
  809.    On operating systems such as UNIX and Windows, all dynamic memory
  810. allocation requests from a process are dealt with by using a feature
  811. called _virtual memory_.  This means that a process cannot perform
  812. illegal requests without them being denied, which protects the other
  813. running processes and the operating system from being affected by such
  814. errors.  However, on AmigaOS and Netware platforms there is no virtual
  815. memory support and so all processes effectively share the same address
  816. space as the operating system and any other running processes.  This
  817. means that one process can accidentally write into the data structures
  818. of another process, usually causing the other process to fail and bring
  819. down the system.  In addition, a process which allocates a lot of memory
  820. will result in there being less available memory for other running
  821. processes, and in extreme cases the operating system itself.
  822.  
  823. 6.1 Virtual memory
  824. ==================
  825.  
  826.    _Virtual memory_ is an operating system feature that was originally
  827. used to provide large usable address spaces for every process on
  828. machines that had very little physical memory.  It is used by an
  829. operating system to fool(1) a running process into believing that it can
  830. allocate a vast amount of memory for its own purposes, although whether
  831. it is allowed to or not depends on the operating system and the
  832. permissions of the individual user.
  833.  
  834.    Virtual memory works by translating a virtual address (which the
  835. process uses) into a physical address (which the operating system
  836. uses).  It is generally implemented via a piece of hardware called a
  837. _memory management unit_, or MMU.  The MMU's primary job is to
  838. translate any virtual addresses that are referred to by machine
  839. instructions into physical addresses by looking up a table which is
  840. built by the operating system.  This table contains mappings to and
  841. from _pages_(2) rather than bytes since it would otherwise be very
  842. inefficient to handle mappings between individual bytes.  As a result,
  843. every virtual memory operation operates on pages, which are indivisible
  844. and are always aligned to the system page size.
  845.  
  846.    Even though each process can now see a huge address space, what
  847. happens when it attempts to allocate more pages than actually
  848. physically exist, or allocate an additional page of memory when all of
  849. the physical pages are in use by it and other processes?  This problem
  850. is solved by the operating system temporarily saving one or more of the
  851. least-used pages (which might not necessarily belong that that process)
  852. to a special place in the file system called a _swap file_, and mapping
  853. the new pages to the physical addresses where the old pages once
  854. resided.  The old pages which have been _swapped out_ are no longer
  855. currently accessible, but their location in the swap file is noted in
  856. the translation table.
  857.  
  858.    However, if one of the pages that has been swapped out is accessed
  859. again, a _page fault_ occurs at the instruction which referred to the
  860. address and the operating system catches this and reloads the page from
  861. the swap file, possibly having to swap out another page to make space
  862. for the new one.  If this occurs too often then the operating system
  863. can slow down, having to constantly swap in and swap out the same pages
  864. over and over again.  Such a problem is called _thrashing_ and can only
  865. really be overcome by using less virtual memory or buying more physical
  866. memory.
  867.  
  868.    It is also possible to take advantage of the virtual memory system's
  869. interaction between physical memory and the file system in program
  870. code, since mapping an existing file to memory means that the usual
  871. file I/O operations can be replaced with memory read and write
  872. operations.  The operating system will work out the optimum way to read
  873. and write any buffers and it means that only one copy of the file
  874. exists in both physical memory and the file system.  Note that this is
  875. how _shared libraries_(3) on UNIX platforms are generally implemented,
  876. with each individual process that uses the shared library having it
  877. mapped to somewhere in its address space.
  878.  
  879.    Another major feature of virtual memory is its ability to read
  880. protect and write protect individual pages of process memory.  This
  881. means that the operating system can control access to different parts
  882. of the address space for each process, and also means that a process
  883. can read and/or write protect an area of memory when it wants to ensure
  884. that it won't ever read or write to it again.  If an illegal memory
  885. access is detected then a _signal_ will be sent to the process, which
  886. can either be caught and handled or will otherwise terminate the
  887. process.  Note that as with all virtual memory operations, this ability
  888. to protect memory only applies to pages, so that it is not possible to
  889. protect individual bytes.
  890.  
  891.    However, some versions of UNIX have programmable software _watch
  892. points_ which are implemented at operating system level.  These are
  893. normally used by debuggers to watch a specified area of memory that is
  894. expected to be read from or written to, but can just as easily be used
  895. to implement memory protection at byte level.  Unfortunately, as this
  896. feature is implemented in software(4) rather than in hardware, watch
  897. points tend to be incredibly slow, mainly as a result of the operating
  898. system having to check every instruction before it is executed.
  899.  
  900.    There is also an additional problem when using watch points, which
  901. is due to misaligned reads from memory.  These can occur with
  902. compiler-generated code or with optimised library routines where memory
  903. read, move or write operations have been optimised to work at word
  904. level rather than byte level.  For example, the `memcpy()' function
  905. would normally be written to copy memory a byte at a time, but on some
  906. systems this can be improved by copying a word at a time.
  907. Unfortunately, care has to be taken when reading and writing such words
  908. as the equivalent bytes may not be aligned on word boundaries.
  909. Technically, reading additional bytes before or after a memory
  910. allocation when they share the same word is legal, but when using watch
  911. points such errors will be picked up.  The mpatrol library replaces
  912. most of the memory operation functions provided by the system libraries
  913. with safer versions, although they may not be as efficient.
  914.  
  915.    An operating system with virtual memory is usually going to run ever
  916. so slightly slower than an operating system without it(5), but the
  917. advantages of virtual memory far outweigh the disadvantages, especially
  918. when used for debugging purposes.
  919.  
  920.    ---------- Footnotes ----------
  921.  
  922.    (1) Well, perhaps that's too harsh a word, but it will certainly seem
  923. that way to a process running on a 32-bit UNIX system with only 4
  924. megabytes of physical memory, and yet it will be able to read from and
  925. write to over 4 gigabytes of virtual memory!
  926.  
  927.    (2) The size of a page varies between operating systems and
  928. processor architectures, but they are generally around 4 or 8 kilobytes
  929. in size, and are always a power of two.
  930.  
  931.    (3) DLLs on Windows platforms.
  932.  
  933.    (4) The operating system is still considered software.
  934.  
  935.    (5) Due to the overhead of having to translate every address and
  936. swap in and out pages -- although memory mapped files will usually be
  937. more efficient than using normal file operations on a system without
  938. virtual memory.
  939.  
  940. 6.2 Call stacks and symbol tables
  941. =================================
  942.  
  943.    As stated in the section on stack memory allocations (*note Stack
  944. memory allocations::), when a function is called, a copy of the
  945. caller's state information (including local variables and registers) is
  946. saved on the stack so that it can be restored when the called function
  947. returns.  On many operating systems there is a _calling convention_(1)
  948. which defines the layout of such stack entries so that code compiled in
  949. different languages and with different compilers can be intermixed.
  950. This usually specifies at which stack offsets the stack pointer,
  951. program counter and local variables for the calling function can be
  952. found, although on some processor architectures the function calling
  953. conventions are specified by the hardware and so the operating system
  954. must use these instead.
  955.  
  956.    On systems that have consistent calling conventions, it is usually
  957. possible to perform call stack _tracebacks_ from within the current
  958. function in order to determine the stack of function calls that led to
  959. the current function.  This is extremely useful for debugging purposes
  960. and is done by examining the current stack frame to see if there is a
  961. pointer to the previous stack frame.  If there is, then it can be
  962. followed to find out all of the state information about the calling
  963. function.  This can be repeated until there are no more stack frames.
  964. This is generally how this information is determined by debuggers when
  965. a call stack traceback is requested.
  966.  
  967.    In addition to the pointer to the previous stack frame, the saved
  968. state information also always contains the saved program counter
  969. register, which contains either the address of the instruction that
  970. performed the function call, or the address of the instruction at which
  971. to continue execution when the called function returns(2).  This
  972. information can be used to identify which function performed the call,
  973. since the address of the instruction must lie between the start and end
  974. of one of the functions in the process.
  975.  
  976.    However, in order to determine this symbolic information, it must be
  977. possible to find out where the start and end addresses of all of the
  978. functions in the process are.  This can usually only be read from
  979. object files, since they contain the symbol tables that were used by
  980. the linker to generate the final executable file for the program.  The
  981. object file's symbol tables normally contain information about the
  982. start address, size, name and visibility of every symbol that was
  983. defined, but this depends on the format of the object file and if the
  984. symbol tables have been stripped from the final executable file.
  985.  
  986.    If the object file was created by a compiler then it may also
  987. contain debugging information that was generated by the compiler for
  988. use with a debugger.  Such information may include a mapping of code
  989. addresses to source lines(3), and this information can be used by the
  990. mpatrol library to provide more meaningful information in call stack
  991. tracebacks.
  992.  
  993.    On systems that support shared libraries, additional work must be
  994. done to determine the symbolic information for all of the functions
  995. which have been defined in them.  The symbols for functions that are
  996. defined in shared libraries normally appear as undefined symbols in the
  997. executable file for the program and so must be searched in the system
  998. in order to get the necessary information.  It is usually necessary to
  999. liaise with the _dynamic linker_(4) on many systems.
  1000.  
  1001.    ---------- Footnotes ----------
  1002.  
  1003.    (1) Usually part of the _Application Binary Interface_, or ABI.
  1004.  
  1005.    (2) Also known as the _return address_.
  1006.  
  1007.    (3) Generally known as a line number table.
  1008.  
  1009.    (4) Which is the part of the operating system that performs the
  1010. run-time linking of shared libraries.
  1011.  
  1012. 6.3 Threads
  1013. ===========
  1014.  
  1015.    On systems with virtual memory, such as UNIX and Windows, user
  1016. programs are run as _processes_ which have their own address space and
  1017. resources.  If a process needs to create sub-processes to perform other
  1018. tasks it must call `fork()' or `spawn()' to create new processes, but
  1019. these new processes do not share the same address space or resources as
  1020. the parent process.  If processes need to share memory they must either
  1021. use a message passing interface or explicitly mark a range of memory as
  1022. shareable.
  1023.  
  1024.    Traditionally, this was not too much of a handicap as parallel
  1025. processing was an expensive luxury and could only be made use of by the
  1026. kernel of such systems.  However, with the birth of fast processors and
  1027. parallel programming, programs could be made to run more efficiently
  1028. and faster on multi-processor systems by having more than one _thread_
  1029. of control.  This was achieved by allowing processes to have more than
  1030. one program counter through which the processor could execute
  1031. instructions, and if one thread of control stalled for a particular
  1032. reason then another could continue without stalling the entire process.
  1033.  
  1034.    Such multithreaded programs allow parallel programming and implicit
  1035. shared memory between threads since all threads in a process share the
  1036. same address space and resources.  This is similar to operating systems
  1037. that have no virtual memory, such as AmigaOS and Netware(1), except
  1038. that once a process terminates, all threads terminate as well and all
  1039. of its resources are still reclaimed.
  1040.  
  1041.    Multithreaded programming generally needs no compiler support, but
  1042. does require some primitive operations to be supported by the operating
  1043. system for a threads library to call.  The functions that are available
  1044. in the threads library provide the means for a process to create and
  1045. destroy threads.  There are currently several popular threads libraries
  1046. available, although the POSIX threads standard remains the definitive
  1047. implementation.
  1048.  
  1049.    It is always important to remember when programming a multithreaded
  1050. application that because all threads in a process share the same
  1051. address space, measures must be taken to prevent threads reading and
  1052. writing global data in a haphazard fashion.  This can either be done by
  1053. locking with semaphores and mutexes, or can be performed by using stack
  1054. variables instead of global variables since every thread has its own
  1055. local stack.  Care must be taken to write re-entrant functions -- i.e.
  1056. a function will give exactly the same result with one thread as it will
  1057. with multiple threads running it at the same time.
  1058.  
  1059.    ---------- Footnotes ----------
  1060.  
  1061.    (1) Where the kernel is effectively a single process running all
  1062. user programs as threads.
  1063.  
  1064. 7 Using mpatrol
  1065. ***************
  1066.  
  1067.    This chapter contains a general description of all of the features
  1068. of mpatrol and how to use them effectively.  You'll also find a
  1069. complete reference for mpatrol in the appendices, but you may wish to
  1070. try out the examples (*note Examples::) and the tutorial (*note
  1071. Tutorial::) before reading further.
  1072.  
  1073. 7.1 Library behaviour
  1074. =====================
  1075.  
  1076.    Most of the behaviour of the mpatrol library can be controlled at
  1077. run-time via options which are read from the `MPATROL_OPTIONS'
  1078. environment variable.  This prevents you having to recompile or relink
  1079. each time you want to change a library setting, and so makes it really
  1080. easy to try out different settings to locate a particular bug.  You
  1081. should know how to set the value of an environment variable on your
  1082. system before you read on.
  1083.  
  1084.    By default, the mpatrol library will attempt to determine the
  1085. minimum required alignment for any generic memory allocation when it
  1086. first initialises itself.  This may be affected by the compiler and its
  1087. settings when the library was built but it should normally reflect the
  1088. minimum alignment required by the processor on your system.  If you
  1089. would prefer a larger (or perhaps even smaller) default alignment you
  1090. may change it at run-time using the `DEFALIGN' option.  The value you
  1091. supply must be in bytes, must be a power of two, and should not be
  1092. larger that the system page size.  If you encounter bus errors due to
  1093. misaligned memory accesses then you should increase this value.
  1094.  
  1095.    On systems that have virtual memory the library will attempt to
  1096. write-protect all of its internal structures when user code is being
  1097. run.  This ensures that it is nearly impossible for a program to
  1098. corrupt any mpatrol library data.  However, unprotecting and then
  1099. protecting the structures at every library call has a slight overhead
  1100. so you may prefer to disable this behaviour by using the `NOPROTECT'
  1101. option.  This has no effect on systems that have no virtual memory.
  1102.  
  1103.    Usually it is desirable for many system library routines to be
  1104. protected from being interrupted by certain signals since they may
  1105. themselves be called from signal handlers.  If this is not the case
  1106. then it may be possible to interrupt the program from within such
  1107. routines, perhaps causing problems if their global variables are left
  1108. in an undefined state.  As the mpatrol library replaces some of these
  1109. system library routines it is also possible to specify that they are
  1110. protected from certain interrupt signals using the `SAFESIGNALS' option.
  1111. However, this can sometimes result in it being hard to interrupt the
  1112. program from the keyboard if a lot of processor time is spent in
  1113. mpatrol routines, which is why this behaviour is disabled by default(1).
  1114.  
  1115.    On UNIX systems, the usual way for malloc libraries to allocate
  1116. memory from the process heap is through the `sbrk()' system call.  This
  1117. allocates memory from a contiguous heap, but has the disadvantage in
  1118. that other library functions may also allocate memory using the same
  1119. function, thus creating holes in the heap.  This is not a problem for
  1120. mpatrol, but you may have a suspicion that your bug is due to a
  1121. function from another library corrupting your data so you may wish to
  1122. use the `USEMMAP' option.  This is only available on systems that have
  1123. the `mmap()' system call and allows mpatrol to allocate all of its
  1124. memory from a part of the process heap that is non-contiguous (i.e.
  1125. each call to `mmap()' may return a block of memory that is completely
  1126. unrelated to that returned by the previous call).
  1127.  
  1128.    By default, every time an mpatrol library function is called the
  1129. library will automatically check the freed memory and overflow buffers
  1130. of every memory allocation, which can slow program execution down,
  1131. especially if you suspect the error you are looking for occurs at the
  1132. 1000th memory allocation, for example.  You can therefore use the
  1133. `CHECK' option to specify a range of memory allocations at which the
  1134. mpatrol library will automatically check the freed memory and overflow
  1135. buffers.  All other allocations that fall outside this range will not
  1136. be checked.
  1137.  
  1138.    If the mpatrol library that was built for your system supports
  1139. reading symbolic information from a program's executable file, but it
  1140. cannot locate the executable file, or you wish to specify an
  1141. alternative, you can use the `PROGFILE' option to do this.  All this
  1142. does is instruct the mpatrol library to read symbols from this file
  1143. instead.  Note that on systems that support dynamic linking, the
  1144. library can also read symbols from a dynamically linked executable file
  1145. that has had its normal symbol table stripped.
  1146.  
  1147.    Finally, a list of all of the recognised options in the mpatrol
  1148. library can be displayed to the standard error file stream by using the
  1149. `HELP' option.  This will not affect the settings of the library in any
  1150. way, so you should be able to use other options at the same time.
  1151.  
  1152.    ---------- Footnotes ----------
  1153.  
  1154.    (1) In mpatrol release 1.0 it was enabled by default.
  1155.  
  1156. 7.2 Logging and tracing
  1157. =======================
  1158.  
  1159.    If you would like to see a complete log of all of the memory
  1160. allocations, reallocations and deallocations performed by your program,
  1161. use the `LOGALL' option.  This provides detailed tracing for each of
  1162. the mpatrol library functions, and a full description of the format of
  1163. such tracing is given in Example 1 (*note Example 1::).  Alternatively,
  1164. you may select one or more types of functions to be traced using the
  1165. `LOGALLOCS', `LOGREALLOCS', `LOGFREES' and `LOGMEMORY' options if you
  1166. feel that the log file is too large when `LOGALL' is used.  By default
  1167. all diagnostics from the mpatrol library get sent to `mpatrol.log' in
  1168. the current directory, but this can be changed using the `LOGFILE'
  1169. option.
  1170.  
  1171.    On systems that support it, every log entry also contains a call
  1172. stack traceback that may also include the names of the symbols that
  1173. appear on the call stack.  If the object file access library that
  1174. mpatrol was built with has support for reading line number tables from
  1175. object files then the `USEDEBUG' option will also try to determine the
  1176. file name and line number for each entry in the call stack, but only if
  1177. the object files contain the relevant debugging information.  This
  1178. information will only be available before program termination and so
  1179. any call stack tracebacks that appear after the library summary will
  1180. not be displayed with their corresponding file name and line number.
  1181. This option will also slow down program execution since a search
  1182. through the line number tables will have to be made every time a call
  1183. stack is displayed.
  1184.  
  1185.    The mpatrol library will always try to display as much useful
  1186. information as possible in this log file, and will always display a
  1187. summary of library settings and statistics when your program terminates
  1188. successfully.  If you don't get this then your program did not call
  1189. `exit()' and either called `abort()' or was terminated by the operating
  1190. system instead.  In such cases, either use a debugger to see where your
  1191. program crashed or use the `LOGALL' option to see the last successful
  1192. library call in the log file so that you have a rough idea of where
  1193. your program crashed.
  1194.  
  1195.    It is also possible to get mpatrol to write more summary information
  1196. to the log file after it writes out its settings and statistics at
  1197. program termination.  Use the `SHOWFREED' and `SHOWUNFREED' options to
  1198. display a list of freed and unfreed memory allocations.  The former
  1199. will only be displayed if the `NOFREE' option is used, but the latter
  1200. can be useful for detecting memory leaks.  The `SHOWMAP' option will
  1201. display a memory map of the heap that was valid when the process
  1202. terminated, and the `SHOWSYMBOLS' option will display any symbolic
  1203. information that the mpatrol library managed to obtain from any
  1204. executable files and libraries that were relevant to the program being
  1205. tested.  All of these options can be selected with the `SHOWALL' option.
  1206.  
  1207. 7.3 General errors
  1208. ==================
  1209.  
  1210.    By default, the mpatrol library follows the guidelines for ANSI C
  1211. regarding the behaviour of the dynamic memory allocation functions it
  1212. replaces(1).  This means that calling `malloc()' with a size of zero is
  1213. allowed, for example.  However, warnings can be generated for all of
  1214. these types of calls by using the `CHECKALL' option.  The `CHECKALLOCS'
  1215. option warns only about calls to `malloc()' and similar functions with
  1216. a size of zero, the `CHECKREALLOCS' option warns only about calls to
  1217. `realloc()' and similar functions with either a null pointer or a size
  1218. of zero, and the `CHECKFREES' option warns only about calls to `free()'
  1219. and similar functions with a null pointer.
  1220.  
  1221.    All newly-allocated memory can be pre-filled with a specified byte
  1222. by using the `ALLOCBYTE' option.  This can be used to catch out code
  1223. that expects newly-allocated memory to be zeroed, although this option
  1224. will have no effect on memory that was allocated with `calloc()'.  All
  1225. free memory can also be pre-filled with a different specified byte by
  1226. using the `FREEBYTE' option.  This will catch out code that expects to
  1227. be able to use the contents of freed memory.
  1228.  
  1229.    Alternatively, the mpatrol library can be instructed to keep all
  1230. freed memory allocations so that its diagnostics can be clearer about
  1231. which freed allocation a piece of code is erroneously trying to access.
  1232. This is controlled with the `NOFREE' option, but since it never reuses
  1233. any freed allocations it can result in a lot more heap memory being
  1234. used.  Note that this option distinguishes between _free_ memory and
  1235. _freed_ memory.  _Free_ memory is unallocated memory that has been
  1236. taken from the system heap.  _Freed_ memory is a freed memory
  1237. allocation, with all of the original details of the allocation
  1238. preserved.
  1239.  
  1240.    Normally, the `NOFREE' option will fill the freed allocation with the
  1241. free byte so that any code that accesses it will hopefully fall over.
  1242. However, the original contents can be preserved using the `PRESERVE'
  1243. option in case you need to see what the contents were just before it
  1244. was freed.  The `NOFREE' option is also affected by the `PAGEALLOC'
  1245. option, since then the freed allocation will have its contents both
  1246. read and write protected so that nothing can access them.  If the
  1247. `PRESERVE' option is used in this case then the freed allocation will
  1248. only be made write-protected so that the original contents can be read
  1249. from but not written to.
  1250.  
  1251.    ---------- Footnotes ----------
  1252.  
  1253.    (1) I attempted to do the same for ANSI C++ but there are still
  1254. namespace and exception handling issues to be resolved.
  1255.  
  1256. 7.4 Overwrites and underwrites
  1257. ==============================
  1258.  
  1259.    Once a block of memory has been allocated, it is imperative that the
  1260. program does not attempt to write any data past the end of the block or
  1261. write any data just before the beginning of the block.  Even writing a
  1262. single byte just beyond the end of an allocation or just before the
  1263. beginning of an allocation can cause havoc.  This is because most
  1264. malloc libraries store the details of the allocated block in the first
  1265. few words before the beginning of the block, such as its size and a
  1266. pointer to the next block.  The mpatrol library does not do this, so a
  1267. program which failed using the normal malloc library and worked when
  1268. the mpatrol library was linked in is a possible candidate for turning
  1269. on overflow buffers.
  1270.  
  1271.    Such memory corruption can be extremely difficult to pinpoint as it
  1272. is unlikely to show itself until the next call is made to the malloc
  1273. library, or if the internal malloc library blocks were not overwritten,
  1274. the next time the data is read from the block that was overwritten.  If
  1275. the former is the case then the next library call will cause an
  1276. internal error or a crash, but only when the memory block that was
  1277. affected is referenced.  This is likely to disappear when using the
  1278. mpatrol library since it keeps its internal structures separate, and
  1279. write-protects them on systems that support memory protection.
  1280.  
  1281.    In order to identify such errors, it is possible to place special
  1282. buffers(1) on either side of every memory allocation, and these will be
  1283. pre-filled with a specified byte.  Before every mpatrol library call,
  1284. the library will check the integrity of every such overflow buffer in
  1285. order to check for a memory underwrite or overwrite.  Depending on the
  1286. number of allocations and size of these buffers, this can take a
  1287. noticable amount of time (which is why overflow buffers are disabled by
  1288. default), but can mean that these errors get noticed sooner.  The
  1289. option which governs this is `OFLOWSIZE'.  The byte with which they get
  1290. pre-filled can be changed with `OFLOWBYTE'.  Depending on what gets
  1291. written, it might only be possible to see such errors when a different
  1292. size of buffer or a different pre-fill byte is used.
  1293.  
  1294.    A worse situation can occur when it is only reads from memory that
  1295. overflow or underflow; i.e. with the faulty code reading just before or
  1296. just past a memory allocation.  These cannot be detected by overflow
  1297. buffers as it is not possible using conventional means to interrupt
  1298. every single read from memory.  However, on systems with virtual
  1299. memory, it is possible to use the memory protection feature to provide
  1300. an alternative to overflow buffers, although at the added expense of
  1301. increased memory usage.
  1302.  
  1303.    The `PAGEALLOC' option turns on this feature and automatically rounds
  1304. up the size of every memory allocation to a multiple of the system page
  1305. size.  It also rounds up the size of every overflow buffer to a
  1306. multiple of the system page size so that every memory allocation
  1307. occupies its own set of pages of virtual memory and no two memory
  1308. allocations occupy the same page of virtual memory.  The overflow
  1309. buffers are then read and write protected so that any memory accesses
  1310. to them will generate an error(2).  Following on from the previous
  1311. section, the `PAGEALLOC' option also causes free memory to be read and
  1312. write protected as well since that will also occupy non-overlapping
  1313. virtual memory pages.
  1314.  
  1315.    The remaining memory that is left over within an allocation's pages
  1316. is effectively turned into traditional overflow buffers, being
  1317. pre-filled with the overflow byte and checked periodically by the
  1318. mpatrol library to ensure that nothing has written into them.  However,
  1319. because of this remaining memory, the library has a choice of where to
  1320. place the memory allocation within its pages.  If it places the
  1321. allocation at the very beginning then it will catch memory underwrites,
  1322. but if it places the allocation at the very end then it will catch
  1323. memory overwrites.  Such a choice can be controlled at run-time by
  1324. supplying an argument to the `PAGEALLOC' option.  If `PAGEALLOC=LOWER'
  1325. is used then every allocation will be placed at the very beginning of
  1326. its pages and if `PAGEALLOC=UPPER' is used then the placement will be
  1327. at the very end of its pages.  This is probably better explained in
  1328. Example 3 (*note Example 3::) where the problems with `PAGEALLOC=UPPER'
  1329. and alignment are also discussed.
  1330.  
  1331.    Obviously, there are still some deficiencies when using `PAGEALLOC'
  1332. since it can use up a huge amount of memory (especially with `NOFREE')
  1333. and the overflow buffers within an allocation's pages can still be read
  1334. without causing an immediate error.  Both of these deficiencies can be
  1335. overcome by using the `OFLOWWATCH' option to install _software watch
  1336. points_ instead of overflow buffers, but there are still very few
  1337. systems that support software watch points at the moment, and it can
  1338. slow a program's execution speed down by a factor of around 10,000.
  1339. The reason for this is that software watch points instruct the
  1340. operating system to check every read from and write to memory, which
  1341. means that it has to single-step through a process checking every
  1342. instruction before it is executed.  However, this is a very thorough
  1343. way of checking for overflows and is unlikely to miss anything,
  1344. although there may be problems with misaligned memory accesses when
  1345. using watch points (*note Virtual memory::).
  1346.  
  1347.    Note that from release 1.1.0 of mpatrol, the library comes with
  1348. replacement functions for many memory operation functions, such as
  1349. `memset()' and `memcpy()'.  These new functions provide additional
  1350. checks to ensure that if a memory operation is being performed on a
  1351. memory block, the operation will not read or write before or beyond the
  1352. boundaries of that block.
  1353.  
  1354.    Normally, if an error is discovered in the call to such functions,
  1355. the mpatrol library will report the error but prevent the operation
  1356. from being performed before continuing execution.  If the error was
  1357. that the range of memory being operated on overflowed the boundaries of
  1358. an existing memory allocation then the `ALLOWOFLOW' option can be used
  1359. to turn the error into a warning and force the operation to continue.
  1360. This behaviour can be desirable in certain cases where third-party
  1361. libraries are being used that make such calls but the end result does
  1362. not overflow the allocation boundary.
  1363.  
  1364.    To conclude, if you suspect your program has a piece of code which
  1365. is performing illegal memory underwrites or overwrites to a memory
  1366. allocation you should use each of the following options in sequence,
  1367. but only if your system supports them.
  1368.  
  1369.   1. `OFLOWSIZE=8'
  1370.  
  1371.   2. `OFLOWSIZE=32'
  1372.  
  1373.   3. `OFLOWSIZE=1' `PAGEALLOC=LOWER'
  1374.  
  1375.   4. `OFLOWSIZE=1' `PAGEALLOC=UPPER'
  1376.  
  1377.   5. `OFLOWSIZE=8' `OFLOWWATCH'
  1378.  
  1379.   6. `OFLOWSIZE=32' `OFLOWWATCH'
  1380.  
  1381.    ---------- Footnotes ----------
  1382.  
  1383.    (1) Commonly known as _overflow buffers_ or _fence posts_.
  1384.  
  1385.    (2) This is a feature that was first used by Electric Fence (*note
  1386. Related software::) to track down memory corruption.
  1387.  
  1388. 7.5 Using with a debugger
  1389. =========================
  1390.  
  1391.    If you would like to use mpatrol to pause at a specific memory
  1392. allocation, reallocation or deallocation in a debugger then this
  1393. section will describe how to go about it.  Unfortunately, debuggers
  1394. vary widely in function and usage and are normally very
  1395. system-dependent.  The example below will use `gdb' as the debugger,
  1396. but as long as you know how to set a breakpoint within a debugger, any
  1397. one will do.
  1398.  
  1399.    First of all, decide where you would like the mpatrol library to
  1400. pause when running your program within the debugger.  You can choose
  1401. one allocation index to break at using the `ALLOCSTOP' option, or you
  1402. can choose to break at a specific reallocation of that allocation by
  1403. also using the `REALLOCSTOP' option.  If you use `REALLOCSTOP' without
  1404. using `ALLOCSTOP' then you will break at the first memory allocation
  1405. that has been reallocated the specified number of times.  You can also
  1406. choose to break at the point in your program that frees a specific
  1407. allocation index by using the `FREESTOP' option.
  1408.  
  1409.    The normal process for determining where you would like to pause
  1410. your program in the debugger is by using the `LOGALL' option and
  1411. examining the log file produced by mpatrol.  If your program crashed
  1412. then you should look at the last entry in the log file to see what the
  1413. allocation index (and possibly also the reallocation index) of the last
  1414. successful call was.  You can then decide which of the above options to
  1415. use.  Note that the debugger will break at a point before any work is
  1416. done by the mpatrol library for that allocation index so that you can
  1417. see if it was the last successful operation that caused the damage.
  1418.  
  1419.    Having decided which combination of mpatrol options to use, you
  1420. should set them in the `MPATROL_OPTIONS' environment variable before
  1421. running the debugger on your program.  Alternatively, your debugger may
  1422. have a command that allows you to modify your environment during
  1423. debugging, but you're just as well setting the environment variable
  1424. before you run the debugger as it shouldn't make any difference(1).
  1425.  
  1426.    After you get to the debugger command prompt, you should set a
  1427. breakpoint at the `__mp_trap()' function.  This is the function that
  1428. gets called when the specified allocation index and/or reallocation
  1429. index appears and so when you run your program under the debugger the
  1430. mpatrol library will call `__mp_trap()' and the debugger will stop at
  1431. that point.  If you are not running your program within a debugger, or
  1432. if you haven't set the breakpoint, then `__mp_trap()' will still be
  1433. called, but it won't do anything.  Note that there may be some naming
  1434. issues on some platforms where the visible name of a global function
  1435. gets an underscore prepended to it.  You may have to take that into
  1436. account when setting the breakpoint on such systems.
  1437.  
  1438.    Now that you have set the `MPATROL_OPTIONS' environment variable and
  1439. have set the debugger to break at `__mp_trap()', all that is required
  1440. is for you to run your program.  Hopefully, the debugger should stop at
  1441. `__mp_trap()'.  If it doesn't then you may have to check your
  1442. environment variable settings to ensure that they are the same as when
  1443. you ran the program outwith the debugger, although obviously with the
  1444. addition of `ALLOCSTOP', etc.  Once the program has been halted by the
  1445. debugger, you can then single-step through your code until you see
  1446. where it goes wrong.  If this is near the end of your program then
  1447. you'll have saved yourself a lot of time by using this method.
  1448.  
  1449.    The following example will be used to illustrate the steps involved
  1450. in using the `ALLOCSTOP', `REALLOCSTOP' and `FREESTOP' options.
  1451. However, it is only for tutorial purposes and the same effect could
  1452. easily be achieved by breaking at line 18 in a debugger because in this
  1453. case it is obvious from the code and the mpatrol log file where it is
  1454. going wrong.  In real programs this is hardly ever the case(2).
  1455.  
  1456.       1  /*
  1457.       2   * Allocates 1000 blocks of 16 bytes, freeing each block immediately
  1458.       3   * after it is allocated, and freeing the last block twice.
  1459.       4   */
  1460.      
  1461.      
  1462.       7  #include "mpatrol.h"
  1463.      
  1464.      
  1465.      10  int main(void)
  1466.      11  {
  1467.      12      void *p;
  1468.      13      int i;
  1469.      
  1470.      15      for (i = 0; i < 1000; i++)
  1471.      16          if (p = malloc(16))
  1472.      17              free(p);
  1473.      18      free(p);
  1474.      19      return EXIT_SUCCESS;
  1475.      20  }
  1476.  
  1477.    Compile this example code with debugging information enabled and
  1478. link it with the mpatrol library, then set `MPATROL_OPTIONS' to
  1479. `LOGALL' and run the resulting program.  If you examine `mpatrol.log'
  1480. you will see the following near the bottom of the file.
  1481.  
  1482.      ...
  1483.      
  1484.      ALLOC: malloc (1000, 16 bytes, 2 bytes) [main|test.c|16]
  1485.              0x80000D8E main
  1486.              0x80000D24 _start
  1487.      
  1488.      returns 0x80033000
  1489.      
  1490.      FREE: free (0x80033000) [main|test.c|17]
  1491.              0x80000DBE main
  1492.              0x80000D24 _start
  1493.      
  1494.          0x80033000 (16 bytes) {malloc:1000:0} [main|test.c|16]
  1495.              0x80000D8E main
  1496.              0x80000D24 _start
  1497.      
  1498.      FREE: free (0x80033000) [main|test.c|18]
  1499.              0x80000DE8 main
  1500.              0x80000D24 _start
  1501.      
  1502.      ERROR: free: 0x80033000 has not been allocated
  1503.      
  1504.      ...
  1505.  
  1506.    In this example, we'll want to use `ALLOCSTOP' to halt the program at
  1507. the 1000th memory allocation so that we can step through it with a
  1508. debugger.  So, set `MPATROL_OPTIONS' to `ALLOCSTOP=1000' and load the
  1509. program into the debugger.  If you are using `gdb' you can now do the
  1510. following steps, but if you are not you will have to use the equivalent
  1511. commands in your debugger.  Note that `(gdb)' is the debugger command
  1512. prompt and so anything that appears on that line after that should be
  1513. typed as a command.
  1514.  
  1515.      (gdb) break __mp_trap
  1516.      Breakpoint 1 at 0x80004026
  1517.      (gdb) run
  1518.      Starting program: a.out
  1519.      Breakpoint 1, 0x80004026 in __mp_trap()
  1520.      (gdb) backtrace
  1521.      #0  0x80004026 in __mp_trap()
  1522.      #1  0x800027ec in __mp_getmemory()
  1523.      #2  0x80001138 in __mp_alloc()
  1524.      #3  0x80000d8e in main() at test.c:16
  1525.      (gdb) finish
  1526.      Run till exit from #0  0x80004026 in __mp_trap()
  1527.      0x800027ec in __mp_getmemory()
  1528.      (gdb) finish
  1529.      Run till exit from #0  0x800027ec in __mp_getmemory()
  1530.      0x80001138 in __mp_alloc()
  1531.      (gdb) finish
  1532.      Run till exit from #0  0x80001138 in __mp_alloc()
  1533.      0x80000d8e in main() at test.c:16
  1534.      16              if (p = malloc(16))
  1535.      (gdb) step
  1536.      17                  free(p);
  1537.      (gdb) step
  1538.      15          for (i = 0; i < 1000; i++)
  1539.      (gdb) step
  1540.      18          free(p);
  1541.      (gdb) quit
  1542.      The program is running.  Quit anyway (and kill it)? (y or n) y
  1543.  
  1544.    After setting the breakpoint and running the program, the debugger
  1545. halts at `__mp_trap()'.  Because `__mp_trap()' is a function within the
  1546. mpatrol library, you don't want to bother stepping through any of the
  1547. library functions, and in this case you can't since the mpatrol library
  1548. was not compiled with debugging information enabled.  So, after
  1549. returning from all of the library functions, the source line becomes
  1550. line 16 because that was the location of the 1000th memory allocation.
  1551. Single-stepping twice gets us to line 18 which is our destination.
  1552.  
  1553.    Sometimes it is useful to be able to see information about a memory
  1554. allocation whilst running a program from within a debugger.  The
  1555. `__mp_printinfo()' function is provided for that purpose and takes a
  1556. heap address as its only argument.  Using the above example, it would
  1557. have been possible to print out information about the pointer `p' at
  1558. line 17 from within `gdb':
  1559.  
  1560.      (gdb) call __mp_printinfo(p)
  1561.      address 0x80033000 located in allocated block:
  1562.          start of block:     0x80033000
  1563.          size of block:      2 bytes
  1564.          allocated by:       malloc
  1565.          allocation index:   1000
  1566.          reallocation index: 0
  1567.          calling function:   main
  1568.          called from file:   test.c
  1569.          called at line:     16
  1570.          function call stack:
  1571.              0x80000D8E main
  1572.              0x80000D24 _start
  1573.  
  1574.    Some debuggers, such as `gdb', also allow you to define your own
  1575. commands for use in a debugging session.  The following example defines
  1576. a new `gdb' command called `printalloc' which calls
  1577. `__mp_printinfo()'(3):
  1578.  
  1579.      (gdb) define printalloc
  1580.      Type commands for definition of "printalloc".
  1581.      End with a line saying just "end".
  1582.      >call __mp_printinfo($arg0)
  1583.      >end
  1584.      (gdb) document printalloc
  1585.      Type documentation for "printalloc".
  1586.      End with a line saying just "end".
  1587.      >Displays information about an address in the heap.
  1588.      >end
  1589.  
  1590.    ---------- Footnotes ----------
  1591.  
  1592.    (1) Unless you've linked the debugger with the mpatrol library.
  1593.  
  1594.    (2) The other reason that this program is simple is because a proper
  1595. example would generally involve crashing the program, but on AmigaOS
  1596. and Netware that would also involve crashing the system -- not
  1597. something you'd want to do whilst trying this out.
  1598.  
  1599.    (3) A sample GDB command file for use with mpatrol can be found in
  1600. `extra/.gdbinit'.
  1601.  
  1602. 7.6 Testing
  1603. ===========
  1604.  
  1605.    The mpatrol library has several features that make it useful when
  1606. testing a program's dynamic memory allocations.  These are features
  1607. that do not help in fixing an existing bug, but rather help to identify
  1608. additional bugs that may be lurking in your code.
  1609.  
  1610.    It is possible to set a simulated upper limit on the amount of heap
  1611. memory available to a process with the `LIMIT' option, which accepts a
  1612. size in bytes, but will be disabled when it is zero.  This can be
  1613. extremely useful for testing a program under simulated low memory
  1614. conditions to see how it handles such errors.  Of course, you should
  1615. set the heap limit to a value less than the amount of actual available
  1616. memory otherwise this option will have no effect.  Note that the
  1617. mpatrol library may use up a small amount of heap memory when it
  1618. initialises itself(1) so the value passed to the `LIMIT' option may
  1619. need to be set slightly higher than you would normally expect.
  1620.  
  1621.    It is also possible to instruct the mpatrol library to randomly fail
  1622. a certain number of memory allocations so that you can further test
  1623. error handling code in a program.  The frequency at which failures
  1624. occur can be controlled with the `FAILFREQ' option, where a value of
  1625. zero means that no failures will occur, but any other value will
  1626. randomly cause failures.  For example, a value of `10' will cause
  1627. roughly one in ten failures and a value of `1' will cause every memory
  1628. allocation to fail.  The random sequence can be made predictable by
  1629. using the `FAILSEED' option.  If this is non-zero then the same program
  1630. run with the same failure frequency and same failure seed will fail on
  1631. exactly the same memory allocations.  If this is zero then the failure
  1632. seed will itself be set randomly, but you can see its value when the
  1633. summary is displayed at program termination.
  1634.  
  1635.    When running _batch tests_(2) it is sometimes useful to be able to
  1636. detect if there have been any memory leaks.  Such leaks should normally
  1637. be distinguished from code which has purposely not freed the memory
  1638. that it allocated, so there may be a certain expected number of unfreed
  1639. allocations at program termination.  It may be that you would like to
  1640. highlight any additional unfreed allocations since they may be due to
  1641. real memory leaks, so the `UNFREEDABORT' option can be set to a
  1642. threshold number of expected unfreed allocations.  If the library
  1643. detects a number of unfreed allocations higher than this then it will
  1644. abort the program at termination so that it fails.  All tests that fail
  1645. in this way can then be examined after the test suite finishes.
  1646.  
  1647.    ---------- Footnotes ----------
  1648.  
  1649.    (1) Actually, it's not really the mpatrol library that uses the
  1650. memory but the object file access libraries since they call `malloc()'
  1651. to allocate any memory that they require.
  1652.  
  1653.    (2) A set of tests that run without user intervention.
  1654.  
  1655. 7.7 Library functions
  1656. =====================
  1657.  
  1658.    Along with the standard set of C and C++ dynamic memory allocation
  1659. functions, the mpatrol library also comes with an additional set of
  1660. functions which can be used to provide additional information to your
  1661. program, and which can be called at various points in your code for
  1662. debugging purposes.  You must always include the `mpatrol.h' header
  1663. file in order to use these functions, but you can check for a specific
  1664. version of the mpatrol library by checking the `MPATROL_VERSION'
  1665. preprocessor macro.
  1666.  
  1667.    It is possible to obtain a great deal of information about an
  1668. existing memory allocation using the `__mp_info()' function.  This
  1669. takes an address as an argument and fills in any details about its
  1670. corresponding memory allocation in a supplied structure.  The following
  1671. example illustrates this (it can be found in `tests/pass/test4.c').
  1672.  
  1673.      23  /*
  1674.      24   * Demonstrates and tests the facility for obtaining information
  1675.      25   * about the allocation a specific address belongs to.
  1676.      26   */
  1677.      
  1678.      
  1679.      29  #include "mpatrol.h"
  1680.      30  #include <stdio.h>
  1681.      
  1682.      
  1683.      33  void display(void *p)
  1684.      34  {
  1685.      35      __mp_allocstack *s;
  1686.      36      __mp_allocinfo d;
  1687.      
  1688.      38      if (!__mp_info(p, &d))
  1689.      39      {
  1690.      40          fprintf(stderr, "nothing known about address 0x%08lX\n", p);
  1691.      41          return;
  1692.      42      }
  1693.      43      fprintf(stderr, "block:   0x%08lX\n", d.block);
  1694.      44      fprintf(stderr, "size:    %lu\n", d.size);
  1695.      45      fprintf(stderr, "type:    %lu\n", d.type);
  1696.      46      fprintf(stderr, "alloc:   %lu\n", d.alloc);
  1697.      47      fprintf(stderr, "realloc: %lu\n", d.realloc);
  1698.      48      fprintf(stderr, "func:    %s\n", d.func ? d.func : "NULL");
  1699.      49      fprintf(stderr, "file:    %s\n", d.file ? d.file : "NULL");
  1700.      50      fprintf(stderr, "line:    %lu\n", d.line);
  1701.      51      for (s = d.stack; s != NULL; s = s->next)
  1702.      52      {
  1703.      53          fprintf(stderr, "\t0x%08lX: ", s->addr);
  1704.      54          fprintf(stderr, "%s\n", s->name ? s->name : "NULL");
  1705.      55      }
  1706.      56      fprintf(stderr, "freed:   %d\n", d.freed);
  1707.      57  }
  1708.      
  1709.      
  1710.      60  void func2(void)
  1711.      61  {
  1712.      62      void *p;
  1713.      
  1714.      64      if (p = malloc(16))
  1715.      65      {
  1716.      66          display(p);
  1717.      67          free(p);
  1718.      68      }
  1719.      69      display(p);
  1720.      70  }
  1721.      
  1722.      
  1723.      73  void func1(void)
  1724.      74  {
  1725.      75      func2();
  1726.      76  }
  1727.      
  1728.      
  1729.      79  int main(void)
  1730.      80  {
  1731.      81      func1();
  1732.      82      return EXIT_SUCCESS;
  1733.      83  }
  1734.  
  1735.    When this is compiled and run, it should give the following output,
  1736. although the pointers are likely to be different.
  1737.  
  1738.      block:   0x8000A068
  1739.      size:    16
  1740.      type:    0
  1741.      alloc:   10
  1742.      realloc: 0
  1743.      func:    func2
  1744.      file:    test4.c
  1745.      line:    64
  1746.               0x80000BEC: func2
  1747.               0x80000C3E: func1
  1748.               0x80000C48: main
  1749.               0x800009E8: _start
  1750.      freed:   0
  1751.      nothing known about address 0x8000A068
  1752.  
  1753.    As you can see, anything that the mpatrol library knows about any
  1754. memory allocation can be obtained for use in your own code, which can
  1755. be very useful if you need to write handlers to keep track of memory
  1756. allocations, etc. for debugging purposes.  It can also be useful to
  1757. have this information when running your program within a debugger, so
  1758. you can use the `__mp_printinfo()' function to display information
  1759. about a heap address if your debugger supports calling functions from
  1760. the command prompt.
  1761.  
  1762.    It is also possible for you to be able to intercept calls to
  1763. allocate, reallocate and deallocate memory for your own purposes.  You
  1764. can install prologue and epilogue functions that the mpatrol library
  1765. will call before and after every time one of its functions is called.
  1766. These can be used for additional tracing or simply to add extra checks
  1767. to your code.  The following code is an example of this and can be
  1768. found in `tests/pass/test2.c'.
  1769.  
  1770.      23  /*
  1771.      24   * Demonstrates and tests the facility for specifying user-defined
  1772.      25   * prologue and epilogue functions.
  1773.      26   */
  1774.      
  1775.      
  1776.      29  #include "mpatrol.h"
  1777.      30  #include <stdio.h>
  1778.      
  1779.      
  1780.      33  void prologue(const void *p, size_t l)
  1781.      34  {
  1782.      35      if (p == (void *) -1)
  1783.      36          fprintf(stderr, "allocating %lu bytes\n", l);
  1784.      37      else if (l == (size_t) -1)
  1785.      38          fprintf(stderr, "freeing allocation 0x%08lX\n", p);
  1786.      39      else if (l == (size_t) -2)
  1787.      40          fprintf(stderr, "duplicating string `%s'\n", p);
  1788.      41      else
  1789.      42          fprintf(stderr, "reallocating allocation 0x%08lX to %lu bytes\n", p, l);
  1790.      43  }
  1791.      
  1792.      
  1793.      46  void epilogue(const void *p)
  1794.      47  {
  1795.      48      if (p != (void *) -1)
  1796.      49          fprintf(stderr, "allocation returns 0x%08lX\n", p);
  1797.      50  }
  1798.      
  1799.      
  1800.      53  int main(void)
  1801.      54  {
  1802.      55      void *p, *q;
  1803.      
  1804.      57      __mp_prologue(prologue);
  1805.      58      __mp_epilogue(epilogue);
  1806.      59      if (p = malloc(16))
  1807.      60          if (q = realloc(p, 32))
  1808.      61              free(q);
  1809.      62          else
  1810.      63              free(p);
  1811.      64      if (p = (char *) strdup("test"))
  1812.      65          free(p);
  1813.      66      return EXIT_SUCCESS;
  1814.      67  }
  1815.  
  1816.    Once again, if you compile and run the above code, you should see
  1817. the following output.
  1818.  
  1819.      allocating 16 bytes
  1820.      allocation returns 0x8000A068
  1821.      reallocating allocation 0x8000A068 to 32 bytes
  1822.      allocation returns 0x8000A068
  1823.      freeing allocation 0x8000A068
  1824.      duplicating string `test'
  1825.      allocation returns 0x8000A068
  1826.      freeing allocation 0x8000A068
  1827.  
  1828.    Along with being able to install prologue and epilogue functions,
  1829. you can also install a low-memory handler with the `__mp_nomemory()'
  1830. function, which will be called by the mpatrol library if it ever runs
  1831. out of memory during the call to a memory allocation function.  This
  1832. gives you the opportunity to use that handler to either free up any
  1833. unneeded memory or simply to abort, thus removing the need to check for
  1834. failed allocations.
  1835.  
  1836.    Finally, there are three functions which affect the mpatrol library
  1837. globally.  The first, `__mp_check()', allows you to force an internal
  1838. check of the mpatrol library's data structures at any point during
  1839. program execution.  The other two functions, `__mp_memorymap()' and
  1840. `__mp_summary()' allow you to force the generation of a memory map or
  1841. library statistics at any point in your program, in much the same way
  1842. as they would normally be displayed at the end of program execution.
  1843.  
  1844. 7.8 Utilities
  1845. =============
  1846.  
  1847.    A command is provided with the mpatrol distribution which can run
  1848. programs that have been linked with the mpatrol library, using a
  1849. combination of mpatrol options that can be set via the command line.
  1850. All of these options but one map directly onto their equivalent
  1851. environment variable settings and exist mainly so that the user does
  1852. not have to manually change the `MPATROL_OPTIONS' environment variable.
  1853.  
  1854.    The one option that is the exception to this is the `-d' option,
  1855. which can be used to run a program under the control of the mpatrol
  1856. library, even if it wasn't originally linked with the mpatrol library.
  1857. This can only be done on systems that support dynamic linking and where
  1858. the dynamic linker recognises the `LD_PRELOAD' or `_RLD_LIST'
  1859. environment variables.  Even then, it can only be used when the program
  1860. that is being run has been dynamically linked with the system C
  1861. library, rather than statically linked.
  1862.  
  1863.    The reason for all of these limitations is that some SVR4 UNIX
  1864. platforms have a special feature in the dynamic linker which can be
  1865. told to override the symbols from one shared library using the symbols
  1866. from another shared library at run-time.  In this case, it involves
  1867. replacing the symbols for `malloc()', etc., in the system C library
  1868. with the mpatrol versions, but only if they were marked as undefined in
  1869. the original executable file and would therefore have to have been
  1870. loaded from `libc.so'.
  1871.  
  1872.    However, if a program qualifies for use with the `-d' option, it
  1873. means that you can trace all of its dynamic memory allocations as well
  1874. as running it with any of the mpatrol library's debugging options.
  1875. This is mainly a _toy_ feature which allows you to view and manipulate
  1876. the dynamic memory allocations of programs that you don't have the
  1877. source for, but in theory it could be quite useful if you need to debug
  1878. a previously released executable and are unable to recompile or relink
  1879. it.
  1880.  
  1881.    Note that the `mpatrol' command must be set up to use the correct
  1882. object file format access libraries that are required for your system
  1883. if you wish to use the `-d' option.  If the mpatrol library was built
  1884. with `FORMAT=FORMAT_ELF32' support then it must be told to preload the
  1885. ELF access library (normally `libelf.so').  If it was built with
  1886. `FORMAT=FORMAT_BFD' support then it must be told to preload the GNU BFD
  1887. access libraries (normally `libbfd.so' and `libiberty.so').  However,
  1888. if these libraries only exist on your system in archive form then you
  1889. must build `libmpatrol.so' with these extra libraries incorporated into
  1890. it so that there are no dependencies on them at run-time.  However,
  1891. there may well be problems if the resulting shared library contains
  1892. position-dependent code from the archive libraries you incorporated.
  1893. The only way to find out is for you to try it and see.
  1894.  
  1895.    In order to build a shared version of the mpatrol library with
  1896. embedded object file format access libraries, you must first modify the
  1897. `Makefile' you would normally use to build the mpatrol library.  At the
  1898. lines where the linker is invoked to build the shared library, you must
  1899. explicitly add any object file format access libraries that you want to
  1900. use at the end of the linker command line.  This ensures that all
  1901. references to such libraries will be resolved at link time rather than
  1902. run time.  You must then edit the file `src/config.h' and remove all of
  1903. the libraries that you embedded from the definition of the
  1904. `MP_PRELOAD_LIBS' preprocessor macro.  Finally, rebuild the shared
  1905. version of the mpatrol library and the `mpatrol' command and see if
  1906. your efforts were worth it.
  1907.  
  1908.    Another utility program that is provided is called `mleak' and is
  1909. useful for detecting memory leaks in log files produced by the mpatrol
  1910. library.  This program should be used if the mpatrol library could not
  1911. finish writing the log file due to abnormal program termination (which
  1912. would prevent the `SHOWUNFREED' option from working), but note that
  1913. some of the unfreed allocations might have been freed if the program
  1914. had terminated successfully.
  1915.  
  1916.    The `mleak' command scans through an mpatrol log file looking for
  1917. lines beginning with `ALLOC:' and `FREE:' but ignores lines beginning
  1918. with `REALLOC:', so only the `LOGALLOCS' and `LOGFREES' options are
  1919. necessary when running a program linked with the mpatrol library.  Note
  1920. that as a result of this, no attempt is made to account for resizing of
  1921. memory allocations and so the total amount of memory used by the
  1922. resulting unfreed allocations may not be entirely accurate.
  1923.  
  1924.    The `mleak' command takes one optional argument which must be a valid
  1925. mpatrol log filename but if it is omitted then it will use
  1926. `mpatrol.log' as the name of the log file to use.  The `mleak' command
  1927. makes two passes over the log file so the file must be
  1928. randomly-accessible.  If the filename argument is given as `-' then the
  1929. standard input file stream will be used as the log file.
  1930.  
  1931. 8 Profiling
  1932. ***********
  1933.  
  1934.    The mpatrol library has the capability to summarise the information
  1935. it accumulated about the behaviour of dynamic memory allocations and
  1936. deallocations over the lifetime of any program that it was linked and
  1937. run with.  This summary shows a rough profile of all memory allocations
  1938. that were made, and is hence called _profiling_.  There are several
  1939. other different kinds of profiling provided with most compilation
  1940. tools, but they generally profile function calls or line numbers in
  1941. combination with the time it takes to execute them.
  1942.  
  1943.    Memory allocation profiling is useful since it allows a programmer
  1944. to see which functions directly allocate memory from the heap, with a
  1945. view to optimising the memory usage or performance of a program.  It
  1946. also summarises any unfreed memory allocations that were present at the
  1947. end of program execution, some of which could be as a result of memory
  1948. leaks.  In addition, a summary of the sizes and distribution of all
  1949. memory allocations and deallocations is available.
  1950.  
  1951.    Only allocations and deallocations are recorded, with each
  1952. reallocation being treated as a deallocation immediately followed by an
  1953. allocation.  For full memory allocation profiling support, call stack
  1954. traversal must be supported in the mpatrol library and all of the
  1955. program's symbols must have been successfully read by the mpatrol
  1956. library before the program was run.  The library will attempt to
  1957. compensate if either of these requirements are not met, but the
  1958. displayed tables may contain less meaningful information.
  1959.  
  1960.    Memory allocation profiling is disabled by default, but can be
  1961. enabled using the `PROF' option.  This writes all of the profiling data
  1962. to a file called `mpatrol.out' in the current directory at the end of
  1963. program execution, but the name of this file can be changed using the
  1964. `PROFFILE' option.  Sometimes it can also be desirable for the mpatrol
  1965. library to write out the accumulated profiling information in the
  1966. middle of program execution rather than just at the end, even if it is
  1967. only partially complete, and this behaviour can be controlled with the
  1968. `AUTOSAVE' option.  This can be particularly useful when running the
  1969. program from within a debugger, when it is necessary to analyse the
  1970. profiling information at a certain point during program execution.
  1971.  
  1972.    When profiling memory allocations, it is necessary to distinguish
  1973. between small, medium, large and extra large memory allocations that
  1974. were made by a function.  The boundaries which distinguish between
  1975. these allocation sizes can be controlled via the `SMALLBOUND',
  1976. `MEDIUMBOUND' and `LARGEBOUND' options, but they default to 32, 256 and
  1977. 2048 bytes respectively, which should suffice for most circumstances.
  1978.  
  1979.    The `mprof' command is a tool designed to read a profiling output
  1980. file produced by the mpatrol library and display the profiling
  1981. information that was obtained.  The profiling information includes
  1982. summaries of all of the memory allocations listed by size and the
  1983. function that allocated them and a list of memory leaks with the call
  1984. stack of the allocating function.
  1985.  
  1986.    Along with the options listed below, the `mprof' command takes one
  1987. optional argument which must be a valid mpatrol profiling output
  1988. filename but if it is omitted then it will use `mpatrol.out' as the
  1989. name of the file to use.  If the filename argument is given as `-' then
  1990. the standard input file stream will be used as the profiling output
  1991. file.
  1992.  
  1993. `-a'
  1994.      Specifies that different call sites from within the same function
  1995.      are to be differentiated and that the names of all functions
  1996.      should be displayed with their call site offset in bytes.  This
  1997.      affects the direct allocation and memory leak tables.
  1998.  
  1999. `-c'
  2000.      Specifies that certain tables should be sorted by the number of
  2001.      allocations or deallocations rather than the total number of bytes
  2002.      allocated or deallocated.  This affects the direct allocation and
  2003.      memory leak tables.
  2004.  
  2005. `-n' <DEPTH>
  2006.      Specifies the maximum stack depth to use when calculating if one
  2007.      call site has the same call stack as another call site.  This also
  2008.      specifies the maximum number of functions to display in a call
  2009.      stack.  If DEPTH is `0' then the call stack depth will be
  2010.      unlimited in size.  The default call stack depth is `1'.  This
  2011.      affects the memory leak table.
  2012.  
  2013. `-V'
  2014.      Displays the version number of the `mprof' command.
  2015.  
  2016.    We'll now look at an example of using the mpatrol library to profile
  2017. the dynamic memory allocations in a program.  However, remember that
  2018. this example will only fully work on your machine if the mpatrol
  2019. library supports call stack traversal and reading symbols from
  2020. executable files on that platform.  If that is not the case then only
  2021. some of the features will be available.
  2022.  
  2023.    The following example program performs some simple calculations and
  2024. displays a list of numbers on its standard output file stream, but it
  2025. serves to illustrate all of the different features of memory allocation
  2026. profiling that mpatrol is capable of.  The source for the program can
  2027. be found in `tests/profile/test1.c'.
  2028.  
  2029.      23  /*
  2030.      24   * Associates an integer value with its negative string equivalent in a
  2031.      25   * structure, and then allocates 256 such pairs randomly, displays them
  2032.      26   * then frees them.
  2033.      27   */
  2034.      
  2035.      
  2036.      30  #include <stdio.h>
  2037.      31  #include <stdlib.h>
  2038.      32  #include <string.h>
  2039.      
  2040.      
  2041.      35  typedef struct pair
  2042.      36  {
  2043.      37      int value;
  2044.      38      char *string;
  2045.      39  }
  2046.      40  pair;
  2047.      
  2048.      
  2049.      43  pair *new_pair(int n)
  2050.      44  {
  2051.      45      static char s[16];
  2052.      46      pair *p;
  2053.      
  2054.      48      if ((p = (pair *) malloc(sizeof(pair))) == NULL)
  2055.      49      {
  2056.      50          fputs("Out of memory\n", stderr);
  2057.      51          exit(EXIT_FAILURE);
  2058.      52      }
  2059.      53      p->value = n;
  2060.      54      sprintf(s, "%d", -n);
  2061.      55      if ((p->string = strdup(s)) == NULL)
  2062.      56      {
  2063.      57          fputs("Out of memory\n", stderr);
  2064.      58          exit(EXIT_FAILURE);
  2065.      59      }
  2066.      60      return p;
  2067.      61  }
  2068.      
  2069.      
  2070.      64  int main(void)
  2071.      65  {
  2072.      66      pair *a[256];
  2073.      67      int i, n;
  2074.      
  2075.      69      for (i = 0; i < 256; i++)
  2076.      70      {
  2077.      71          n = (int) ((rand() * 256.0) / (RAND_MAX + 1.0)) - 128;
  2078.      72          a[i] = new_pair(n);
  2079.      73      }
  2080.      74      for (i = 0; i < 256; i++)
  2081.      75          printf("%3d: %4d -> \"%s\"\n", i, a[i]->value, a[i]->string);
  2082.      76      for (i = 0; i < 256; i++)
  2083.      77          free(a[i]);
  2084.      78      return EXIT_SUCCESS;
  2085.      79  }
  2086.  
  2087.    After the above program has been compiled and linked with the
  2088. mpatrol library, it should be run with the `PROF' option set in the
  2089. `MPATROL_OPTIONS' environment variable.  Note that `mpatrol.h' was not
  2090. included as it is not necessary for profiling purposes.
  2091.  
  2092.    If all went well, a list of numbers should be displayed on the
  2093. screen and a file called `mpatrol.out' should have been produced in the
  2094. current directory.  This is a binary file containing the total amount
  2095. of profiling information that the mpatrol library gathered while the
  2096. program was running, but it contains concise numerical data rather than
  2097. human-readable data.  To make use of this file, the `mprof' command
  2098. must be run.  An excerpt from the output produced when running `mprof'
  2099. with no options is shown below.
  2100.  
  2101.                                   ALLOCATION BINS
  2102.      
  2103.                                (number of bins: 1024)
  2104.      
  2105.                        allocated                          unfreed
  2106.             --------------------------------  --------------------------------
  2107.       size   count       %     bytes       %   count       %     bytes       %
  2108.      
  2109.          2       8    1.56        16    0.54       8    3.12        16    1.70
  2110.          3      99   19.34       297    9.94      99   38.67       297   31.60
  2111.          4     118   23.05       472   15.80     118   46.09       472   50.21
  2112.          5      31    6.05       155    5.19      31   12.11       155   16.49
  2113.          8     256   50.00      2048   68.54       0    0.00         0    0.00
  2114.      
  2115.      total     512              2988             256               940
  2116.  
  2117.                                  DIRECT ALLOCATIONS
  2118.      
  2119.                       (0 < s <= 32 < m <= 256 < l <= 2048 < x)
  2120.      
  2121.             allocated                       unfreed
  2122.      --------------------------  --------------------------
  2123.      bytes       %   s  m  l  x  bytes       %   s  m  l  x  count  function
  2124.      
  2125.       2988  100.00  %%             940  100.00  %%             512  new_pair
  2126.      
  2127.       2988          %%             940          %%             512  total
  2128.  
  2129.                                     MEMORY LEAKS
  2130.      
  2131.                               (maximum stack depth: 1)
  2132.      
  2133.                      unfreed                      allocated
  2134.      ----------------------------------------  ----------------
  2135.           %     bytes       %   count       %     bytes   count  function
  2136.      
  2137.      100.00       940   31.46     256   50.00      2988     512  new_pair
  2138.      
  2139.                   940   31.46     256   50.00      2988     512  total
  2140.  
  2141.    The first table shown is the allocation bin table which summarises
  2142. the sizes of all objects that were dynamically allocated throughout the
  2143. lifetime of the program.  In this particular case, counts of all
  2144. allocations and deallocations of sizes 1 to 1023 bytes were recorded by
  2145. the mpatrol library in their own specific _bin_ and this information
  2146. was written to the profiling output file.  Allocations and
  2147. deallocations of sizes larger than or equal to 1024 bytes are counted
  2148. as well and the total number of bytes that they represent are also
  2149. recorded.  This information can be extremely useful in understanding
  2150. which sizes of data structures are allocated most during program
  2151. execution, and where changes might be made to make more efficient use
  2152. of the dynamically allocated memory.
  2153.  
  2154.    As can be seen from the allocation bin table, 8 allocations of 2
  2155. bytes, 99 allocations of 3 bytes, 118 allocations of 4 bytes, 31
  2156. allocations of 5 bytes and 256 allocations of 8 bytes were made during
  2157. the execution of the program.  However, all of these memory allocations
  2158. except the 8 byte allocations were still not freed by the time the
  2159. program terminated, resulting in a total memory leak of 940 bytes.
  2160.  
  2161.    The next table shown is the direct allocation table which lists all
  2162. of the functions that allocated memory and how much memory they
  2163. allocated.  The `s m l x' columns represent _small_, _medium_, _large_
  2164. and _extra large_ memory allocations, which in this case are 0 bytes is
  2165. less than a small allocation, which is less than or equal to 32 bytes,
  2166. which is less than a medium allocation, which is less than or equal to
  2167. 256 bytes, which is less than a large allocation, which is less than or
  2168. equal to 2048 bytes, which is less than an extra large allocation.  The
  2169. numbers listed under these columns represent a percentage of the
  2170. overall total and are listed as `%%' if the percentage is 100% or as
  2171. `.' if the percentage is less than 1%.  Percentages of 0% are not
  2172. displayed.
  2173.  
  2174.    The information displayed in the direct allocation table is useful
  2175. for seeing exactly which functions in a program directly perform memory
  2176. allocation, and can quickly highlight where optimisations can be made
  2177. or where functions might be making unnecessary allocations.  In the
  2178. example, this table shows us that 2988 bytes were allocated over 512
  2179. calls by `new_pair()' and that 940 bytes were left unfreed at program
  2180. termination.  All of the allocations that were made by `new_pair()'
  2181. were between 1 and 32 bytes in size.
  2182.  
  2183.    We could now choose to sort the direct allocation table by the
  2184. number of calls to allocate memory, rather than the number of bytes
  2185. allocated, with the `-c' option to `mprof', but that is not relevant in
  2186. this example.  However, we know that there are two calls to allocate
  2187. memory from `new_pair()', so we can use the `-a' option to `mprof' to
  2188. show all call sites within functions rather than just the total for each
  2189. function.  This option does not affect the allocation bin table so the
  2190. new output from `mprof' with the `-a' option looks like:
  2191.  
  2192.                                  DIRECT ALLOCATIONS
  2193.      
  2194.                       (0 < s <= 32 < m <= 256 < l <= 2048 < x)
  2195.      
  2196.             allocated                       unfreed
  2197.      --------------------------  --------------------------
  2198.      bytes       %   s  m  l  x  bytes       %   s  m  l  x  count  function
  2199.      
  2200.       2048   68.54  69               0    0.00                 256  new_pair+14
  2201.        940   31.46  31             940  100.00  %%             256  new_pair+110
  2202.      
  2203.       2988          %%             940          %%             512  total
  2204.  
  2205.                                     MEMORY LEAKS
  2206.      
  2207.                               (maximum stack depth: 1)
  2208.      
  2209.                      unfreed                      allocated
  2210.      ----------------------------------------  ----------------
  2211.           %     bytes       %   count       %     bytes   count  function
  2212.      
  2213.      100.00       940  100.00     256  100.00       940     256  new_pair+110
  2214.      
  2215.                   940   31.46     256   50.00      2988     512  total
  2216.  
  2217.    The names of the functions displayed in the above tables now have a
  2218. byte offset appended to them to indicate at what position in the
  2219. function a call to allocate memory occurred(1).  Now it is possible to
  2220. see that the first call to allocate memory from within `new_pair()' has
  2221. had all of its memory freed, but the second call (from `strdup()') has
  2222. had none of its memory freed.
  2223.  
  2224.    This is also visible in the next table, which is the memory leak
  2225. table and lists all of the functions that allocated memory but did not
  2226. free all of their memory during the lifetime of the program.  The
  2227. default behaviour of `mprof' is to show only the function that directly
  2228. allocated the memory in the memory leak table, but this can be changed
  2229. with the `-n' option.  This accepts an argument specifying the maximum
  2230. number of functions to display in one call stack, with zero indicating
  2231. that all functions in a call stack should be displayed.  This can be
  2232. useful for tracing down the functions that were indirectly responsible
  2233. for the memory leak.  The new memory leak table displayed by `mprof'
  2234. with the `-a' and `-n0' options looks like:
  2235.  
  2236.                                     MEMORY LEAKS
  2237.      
  2238.                               (maximum stack depth: 0)
  2239.      
  2240.                      unfreed                      allocated
  2241.      ----------------------------------------  ----------------
  2242.           %     bytes       %   count       %     bytes   count  function
  2243.      
  2244.      100.00       940  100.00     256  100.00       940     256  new_pair+110
  2245.                                                                  main+88
  2246.                                                                  _start+68
  2247.      
  2248.                   940   31.46     256   50.00      2988     512  total
  2249.  
  2250.    Now that we know where the memory leak is coming from, we can fix it
  2251. by freeing the string as well as the structure at line 77.  A version
  2252. of the above program that does not contain the memory leak can be found
  2253. in `tests/profile/test2.c'.
  2254.  
  2255.    Much of the functionality of this implementation of memory
  2256. allocation profiling is based upon `mprof' by Benjamin Zorn and Paul
  2257. Hilfinger, which was written as a research project and ran on MIPS,
  2258. SPARC and VAX machines.  However, the profiling output files are
  2259. incompatible, the tables displayed have a different format, and the way
  2260. they are implemented is entirely different.
  2261.  
  2262.    ---------- Footnotes ----------
  2263.  
  2264.    (1) If no symbols could be read from the program's executable file,
  2265. or if the corresponding symbol could not be determined, then the
  2266. function names will be replaced with the code addresses at which the
  2267. calls took place.
  2268.  
  2269. 9 Improving performance
  2270. ***********************
  2271.  
  2272.    Because of their need to cover every eventuality, malloc library
  2273. implementations are very general and most do their job well when you
  2274. consider what is thrown at them.  However, your program may not be
  2275. performing as well as it should simply because there may be a more
  2276. efficient way of dealing with dynamic memory allocations.  Indeed,
  2277. there may even be a more efficient malloc library available for you to
  2278. use.
  2279.  
  2280.    If you need to allocate lots of blocks of the same size(1), but you
  2281. won't know the number of blocks you'll require until run-time then you
  2282. could take the easy approach by simply allocating a new block of memory
  2283. for each occurrence.  However, this is going to create a lot of
  2284. (typically small) memory blocks that the underlying malloc library will
  2285. have to keep track of, and even in many good malloc libraries this is
  2286. likely to cause memory fragmentation and possibly even result in the
  2287. blocks scattered throughout the address space rather than all in the
  2288. one place, which is not necessarily a good thing on systems with
  2289. virtual memory.
  2290.  
  2291.    An alternative approach would be to allocate memory in multiples of
  2292. the block size, so that several blocks would be allocated at once.
  2293. This would require slightly more work on your part since you would need
  2294. to write interface code to return a single block, while possible
  2295. allocating space for more blocks if no free blocks were available.
  2296. However, this approach has several advantages.  The first is that the
  2297. malloc library only needs to keep track of a few large allocations
  2298. rather than lots of small allocations, so splitting and merging free
  2299. blocks is less likely to occur.  Secondly, your blocks will be
  2300. scattered about less in the address space of the process, which means
  2301. that on systems with virtual memory there are less likely to be page
  2302. faults if you need to access or traverse all of the blocks you have
  2303. created.
  2304.  
  2305.    A memory allocation concept that is similar to this is called an
  2306. _arena_.  This datatype requires functions which are built on top of
  2307. the existing malloc library functions and which associate each memory
  2308. allocation with a particular arena.  An arena can have as many
  2309. allocations added to it as required, but allocations cannot usually be
  2310. freed until the whole arena is freed.  Note that there are not really
  2311. any generic implementations of arenas that are available as everyone
  2312. tends to write their own version when they require it, although Digital
  2313. UNIX and SGI IRIX systems do come with an arena library called
  2314. _amalloc_.
  2315.  
  2316.    However, what if you don't plan to free all of the blocks at the
  2317. same time?  A slight modification to the above design could be to have
  2318. a _slot table_.  This would involve allocating chunks of blocks as they
  2319. are required, adding each individual block within a chunk to a
  2320. singly-linked list of free blocks.  Then, as new blocks are required,
  2321. the allocator would simply choose the first block on the free list,
  2322. otherwise it would allocate memory for a new chunk of blocks and add
  2323. them to the free list.  Freeing individual blocks would simply involve
  2324. returning the block to the free list.  If this description isn't clear
  2325. enough, have a look in `src/slots.h' and `src/slots.c'.  This is how the
  2326. mpatrol library allocates memory from the system for all of its internal
  2327. structures.  For variable-sized structures, a slightly different
  2328. approach needs to be taken, but for an example of this using strings
  2329. see `src/strtab.h' and `src/strtab.c'.
  2330.  
  2331.    Another optimisation that is possible on UNIX and Windows platforms
  2332. is making use of memory-mapped files.  This allows you to map a
  2333. filesystem object into the address space of your process, thus allowing
  2334. you to treat a file as an array of bytes.  Because it uses the virtual
  2335. memory system to map the file, any changes you make to the mapped
  2336. memory will be applied to the file.  This is implemented through the
  2337. virtual memory system treating the file as a pseudo swap file and will
  2338. therefore only use up physical memory when pages are accessed.  It also
  2339. means that file operations can be replaced by memory read and write
  2340. operations, leading to a very fast and efficient way of performing I/O.
  2341. Another added bonus of this system means that entire blocks of process
  2342. memory can be written to a file for later re-use, just as long as the
  2343. file can later be mapped to the same address.  This can be a lot faster
  2344. than writing to and reading from a specific format of file.
  2345.  
  2346.    If you really don't want to keep track of dynamic memory allocations
  2347. at all then perhaps you should consider _garbage collection_.  This
  2348. allows you to make dynamic memory allocations that need not necessarily
  2349. be matched by corresponding calls to free these allocations.  A garbage
  2350. collector will (at certain points during program execution) attempt to
  2351. look for memory allocations that are no longer referenced by the
  2352. program and free them for later re-use, hence removing all possibility
  2353. of memory leaks.  However, the garbage collection process can take a
  2354. sizable chunk of processor time depending on how large the program is,
  2355. so it is not really an option for real-time programming.  It is also
  2356. very platform-dependent as it examines very low-level structures within
  2357. a process in order to determine which pointers point to which memory
  2358. allocations.  But there is at least one garbage collector(2) that works
  2359. well with C and C++ and acts as a replacement for `malloc()' and
  2360. `free()', so it may be the ideal solution for you.
  2361.  
  2362.    If you do choose to use an alternative malloc library make sure that
  2363. you have a license to do so and that you follow any distribution
  2364. requirements.  On systems that support dynamic linking you may want to
  2365. link the library statically rather than dynamically so that you don't
  2366. have to worry about an additional file that would need to be installed.
  2367. However, whether you have that choice depends on the license for the
  2368. specific library, and some licenses also require that the source code
  2369. for the library be made readily available.  Shared libraries have the
  2370. advantage that they can be updated with bug fixes so that all programs
  2371. that require these libraries will automatically receive these fixes
  2372. without needing to be relinked.
  2373.  
  2374.    If all of the above suggestions do not seem to help and you still
  2375. feel that you have a performance bottleneck in the part of your code
  2376. that deals with dynamically allocated memory then you should try using
  2377. the memory allocation profiling feature of mpatrol.  This can be used
  2378. at run-time to analyse the dynamic memory allocation calls that your
  2379. program makes during its execution, and builds statistics for later
  2380. viewing with the `mprof' command.  It is then possible for you to see
  2381. exactly how many calls were made to each function and where they came
  2382. from.  Such information can then be put to good use in order to
  2383. optimise the relevant parts of your code.
  2384.  
  2385.    And finally, some tips on how to correctly use dynamic memory
  2386. allocations.  The first, most basic rule is to _always_ check the
  2387. return values from `malloc()' and related functions.  _Never_ assume
  2388. that a call to `malloc()' will succeed, because you're unlikely to be
  2389. able to read the future(3).  Alternatively, use (or write) an
  2390. `xmalloc()' or similar function, which calls `malloc()' but never
  2391. returns `NULL' since it will abort instead.  With the C++ operators it
  2392. is slightly different because some versions use exceptions to indicate
  2393. failure, so you should always provide a handler to deal with this
  2394. eventuality.
  2395.  
  2396.    Never use _features_(4) of specific malloc libraries if you want
  2397. your code to be portable.  Always follow the ANSI C or C++ calling
  2398. conventions and never make assumptions about the function or operator
  2399. you are about to call -- the standards committees went to great lengths
  2400. to explicitly specify its behaviour.  For example, don't assume that
  2401. the contents of a freed memory allocation will remain valid until the
  2402. next call to `malloc()', and don't assume that the contents of a newly
  2403. allocated memory block will be zeroed unless you created it with
  2404. `calloc()'.
  2405.  
  2406.    Finally, try stress-testing your program in low memory conditions.
  2407. The mpatrol library contains the `LIMIT' option which can place an
  2408. upper bound on the size of the heap, and also contains the `FAILFREQ'
  2409. and `FAILSEED' options which can cause random memory allocation
  2410. failures.  Doing this will test parts of your code that you would
  2411. probably never expect to be called, but perhaps they will one day!  Who
  2412. would you rather have debugging your program -- yourself or the user?
  2413.  
  2414.    ---------- Footnotes ----------
  2415.  
  2416.    (1) Such as for use in a linked list.
  2417.  
  2418.    (2) A freely distributably library called GC (*note Related
  2419. software::).
  2420.  
  2421.    (3) If you can, why are you reading this -- you've already read it!
  2422.  
  2423.    (4) Whether they are documented or not.
  2424.  
  2425. 10 How it works
  2426. ***************
  2427.  
  2428.    The mpatrol library was originally written with the intention of
  2429. plugging it into an existing compiler so that the compiler could plant
  2430. calls to it in the code it generated when a specific debugging option
  2431. was used.  These extra calls would obviously slow the code down, but
  2432. along with the stack checking options that would be provided, this
  2433. would give the user an enhanced run-time debugging environment.
  2434. Unfortunately, this integration never happened, but the way that
  2435. mpatrol works is still significantly different from other malloc tracing
  2436. libraries.
  2437.  
  2438.    In order to quickly determine exactly which memory allocation a heap
  2439. address belonged to it was necessary to be able to search the heap in
  2440. an efficient manner.  The traditional way of searching along a linked
  2441. list was unfeasible, so an implementation based on _red-black trees_
  2442. was used, where every known memory allocation in the heap was given an
  2443. entry in the tree, with their start addresses as the key.  Another
  2444. major design decision was to also choose red-black trees to implement
  2445. the _best fit_ allocation algorithm.  Although _first fit_ was
  2446. considered, I decided that best fit would allow the library to have
  2447. more control over the heap, with every free memory block in the heap
  2448. given an entry in the free tree, with their sizes as the key.  There
  2449. was a bit of work involved in getting the splitting and merging of free
  2450. blocks to work efficiently, but it seems to work well now.
  2451.  
  2452.    My original implementation had all of the information about each
  2453. memory block stored just before the block itself.  I eventually dropped
  2454. that behaviour in favour of storing all of the library's internal
  2455. information in a separate part of the heap.  I did that for two
  2456. reasons.  The first was because of the problems that would occur due to
  2457. memory allocations with different alignment requirements.  The second
  2458. reason was that the library's internal structures could be
  2459. write-protected on systems with virtual memory, to prevent user code
  2460. interfering with the operation of the library.
  2461.  
  2462.    Because the library attempts to record as much information as
  2463. possible about every memory allocation there will inevitably be a much
  2464. larger memory requirement when running a program linked with the
  2465. library.  This will typically be two or three times larger in
  2466. magnitude, but will be affected by the number of memory allocations
  2467. made and also the number of symbols read.  The latter will also affect
  2468. how quickly the program starts since the first call to allocate memory
  2469. will result in the initialisation of the library and the loading of
  2470. symbols from the executable file and any shared libraries.
  2471.  
  2472.    Due to its design, it is also possible to allocate memory from the
  2473. heap using the mpatrol library functions whilst already within an
  2474. mpatrol library function.  This does not normally occur, but on some
  2475. platforms calling `printf()' from within the library may result in
  2476. `printf()' calling `malloc()' to allocate itself a buffer, which ends
  2477. up as a recursive call.  Luckily, this is dealt with by simply not
  2478. displaying the allocation in the log file, but all other details of the
  2479. allocation are still recorded.  This can sometimes result in _hidden_
  2480. memory usage which occurs behind the scenes and alters the peak memory
  2481. usage in the summary.  This is particularly evident when the library
  2482. uses an object file access library to read program symbols at the time
  2483. of library initialisation.
  2484.  
  2485.    Memory allocation profiling support was added for mpatrol release
  2486. 1.2.0.  Every allocation and deallocation is recorded, with the call
  2487. stack information being used to differentiate all of the call sites
  2488. within the program.  Unlike other profilers that come with UNIX
  2489. systems, even the symbolic information about the program being run is
  2490. written to the profiling output file, since it makes no sense for
  2491. `mprof' to re-read the symbol table from the executable file when it
  2492. has already been read and processed by the mpatrol library.  It also has
  2493. the added bonus of allowing the user to save profiling output files for
  2494. later use even when the executable files which produced them have
  2495. changed or no longer exist.
  2496.  
  2497.    The library is written in a modular fashion so as to make it easy to
  2498. add new functionality.  New modules have already been added, such as
  2499. the _stack_, _symbol_ and _profile_ modules.  Extra information about
  2500. each memory allocation can be added to the _allocation information_
  2501. module in `src/info.h' and `src/info.c' without having to change much
  2502. code in any other files.
  2503.  
  2504. 11 Examples
  2505. ***********
  2506.  
  2507.    Following are a set of examples that are intended to illustrate what
  2508. exactly is possible with the mpatrol library and how to go about using
  2509. it effectively.
  2510.  
  2511.    You should already have built and installed the library and should
  2512. know how to link programs with the library.  Unfortunately, it isn't
  2513. possible to give specific instructions on how to do this as it varies
  2514. from system to system and also depends on your preferred compiler and
  2515. development tools.
  2516.  
  2517.    However, on a typical SVR4 UNIX system, with mpatrol installed in
  2518. `/usr/local', the mpatrol library can usually be incorporated into a
  2519. program using the following commands:
  2520.  
  2521.    * If the mpatrol library was built with no support for any object
  2522.      file format or was built with support for the COFF object file
  2523.      format:
  2524.  
  2525.           cc -I/usr/local/include <file> -L/usr/local/lib -lmpatrol
  2526.  
  2527.    * If the mpatrol library was built with support for the ELF32 object
  2528.      file format access library:
  2529.  
  2530.           cc -I/usr/local/include <file> -L/usr/local/lib -lmpatrol -lelf
  2531.  
  2532.    * If the mpatrol library was built with support for the GNU BFD
  2533.      object file format access library:
  2534.  
  2535.           cc -I/usr/local/include <file> -L/usr/local/lib -lmpatrol -lbfd
  2536.              -liberty
  2537.  
  2538.    If you need to link with other libraries, make sure that they don't
  2539. contain definitions of `malloc()', etc., or if they do then you must
  2540. ensure that the mpatrol library appears before them on the link line.
  2541.  
  2542.    You should also know how to set an environment variable on your
  2543. specific system.  Again, this varies from system to system and also
  2544. depends on the command line interpreter or shell that you use.  The
  2545. environment variable that the mpatrol library uses is called
  2546. `MPATROL_OPTIONS'.  You can see exactly what options are available for
  2547. this environment variable by setting it to `HELP' and then running a
  2548. program that has been linked with the library.
  2549.  
  2550. 11.1 Getting started
  2551. ====================
  2552.  
  2553.    The first example we'll look at is when the argument in a call to
  2554. `free()' doesn't match the return value from `malloc()', even though
  2555. the intention is to free the memory that was allocated by `malloc()'.
  2556. This example is in `tests/fail/test1.c' and causes many existing
  2557. `malloc()' implementations to crash.
  2558.  
  2559.    Along the way, I'll try to describe as many features of the mpatrol
  2560. library as possible, and illustrate them with examples.  Note that the
  2561. output from your version of the library is likely to vary slightly from
  2562. that shown in the examples, especially on non-UNIX systems.
  2563.  
  2564.      23  /*
  2565.      24   * Allocates a block of 16 bytes and then attempts to free the
  2566.      25   * memory returned at an offset of 1 byte into the block.
  2567.      26   */
  2568.      
  2569.      
  2570.      29  #include "mpatrol.h"
  2571.      
  2572.      
  2573.      32  int main(void)
  2574.      33  {
  2575.      34      char *p;
  2576.      
  2577.      36      if (p = (char *) malloc(16))
  2578.      37          free(p + 1);
  2579.      38      return EXIT_SUCCESS;
  2580.      39  }
  2581.  
  2582.    Note that I've removed the copyright message from the start of the
  2583. file and added line numbers so that the tracing below makes more sense.
  2584.  
  2585.    After compiling and linking the above program with the mpatrol
  2586. library, the `MPATROL_OPTIONS' environment variable should be set to be
  2587. `LOGALL' and the program should be executed, generating the following
  2588. output in `mpatrol.log'.
  2589.  
  2590.      @(#) mpatrol 1.2.0 (00/05/16)
  2591.      Copyright (C) 1997-2000 Graeme S. Roy
  2592.      
  2593.      This is free software, and you are welcome to redistribute it under
  2594.      certain conditions; see the GNU Library General Public License for
  2595.      details.
  2596.      
  2597.      For the latest mpatrol release and documentation,
  2598.      visit http://www.cbmamiga.demon.co.uk/mpatrol.
  2599.      
  2600.      Log file generated on Tue May  2 23:41:04 2000
  2601.      
  2602.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test1.c|36]
  2603.              0x00010AE0 main
  2604.              0x000109D4 _start
  2605.      
  2606.      returns 0x00028000
  2607.      
  2608.      FREE: free (0x00028001) [main|test1.c|37]
  2609.              0x00010B24 main
  2610.              0x000109D4 _start
  2611.      
  2612.      ERROR: free: 0x00028001 does not match allocation of 0x00028000
  2613.          0x00028000 (16 bytes) {malloc:13:0} [main|test1.c|36]
  2614.              0x00010AE0 main
  2615.              0x000109D4 _start
  2616.      
  2617.      system page size:  8192 bytes
  2618.      default alignment: 8 bytes
  2619.      overflow size:     0 bytes
  2620.      overflow byte:     0xAA
  2621.      allocation byte:   0xFF
  2622.      free byte:         0x55
  2623.      allocation stop:   0
  2624.      reallocation stop: 0
  2625.      free stop:         0
  2626.      unfreed abort:     0
  2627.      small boundary:    32
  2628.      medium boundary:   256
  2629.      large boundary:    2048
  2630.      lower check range: -
  2631.      upper check range: -
  2632.      failure frequency: 0
  2633.      failure seed:      533453
  2634.      prologue function: <unset>
  2635.      epilogue function: <unset>
  2636.      handler function:  <unset>
  2637.      log file:          mpatrol.log
  2638.      profiling file:    mpatrol.out
  2639.      program filename:  ./test1
  2640.      symbols read:      3240
  2641.      autosave count:    0
  2642.      allocation count:  13
  2643.      allocation peak:   4720 bytes
  2644.      allocation limit:  0 bytes
  2645.      allocated blocks:  1 (16 bytes)
  2646.      freed blocks:      0 (0 bytes)
  2647.      free blocks:       1 (8176 bytes)
  2648.      internal blocks:   25 (204800 bytes)
  2649.      total heap usage:  212992 bytes
  2650.      total compared:    0 bytes
  2651.      total located:     0 bytes
  2652.      total copied:      0 bytes
  2653.      total set:         0 bytes
  2654.      total warnings:    0
  2655.      total errors:      1
  2656.  
  2657.    Ignoring the copyright blurb at the top, let's first take a look at
  2658. the initial log message from the library.  I've annotated each of the
  2659. items with a number that corresponds to the descriptions below.
  2660.  
  2661.       (1)    (2)   (3)    (4)       (5)     (6)    (7)  (8)
  2662.        |      |     |      |         |       |      |    |
  2663.        V      V     V      V         V       V      V    V
  2664.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test1.c|36]
  2665.       (9) -> 0x00010AE0 main
  2666.              0x000109D4 _start <- (10)
  2667.      
  2668.      returns 0x00028000 <- (11)
  2669.  
  2670.   1. Allocation type.  This generalises the type of dynamic memory
  2671.      operation that is being performed, and can be one of `ALLOC',
  2672.      `REALLOC' or `FREE'.  This should make looking for all
  2673.      allocations, reallocations or frees in the log file a lot easier.
  2674.      Alternatively, if a memory operation function was called then this
  2675.      can also be one of `MEMSET', `MEMCOPY', `MEMFIND' or `MEMCMP'.
  2676.  
  2677.   2. Allocation function.  This is the name of the function that has
  2678.      been called to allocate the memory, in this case `malloc'.
  2679.  
  2680.   3. Allocation index.  This is incremented every time a new memory
  2681.      allocation is requested, and persists even if the memory
  2682.      allocation is resized with `realloc()', `recalloc()' or
  2683.      `expand()', so can be useful to keep track of a memory allocation,
  2684.      even if its start address changes.  The mpatrol library may use up
  2685.      the first few allocation indices when it gets initialised.
  2686.  
  2687.   4. Size of requested allocation.
  2688.  
  2689.   5. Alignment for requested allocation.  This is normally the default
  2690.      system alignment for general-purpose memory allocations, but may
  2691.      be different depending on the type of function that is used to
  2692.      allocate the memory.
  2693.  
  2694.    The following information contains source file details of where the
  2695. call to `malloc()' came from, but is only available if the source file
  2696. containing the call to `malloc()' included `mpatrol.h'; otherwise the
  2697. fields will all be `-'(1).  Because of the convoluted way this
  2698. information is obtained for the C++ operators, you may encounter some
  2699. problems in existing C++ programs when making direct calls to `operator
  2700. new' for example.  However, if you want to disable the redefinition of
  2701. the C++ operators in `mpatrol.h' you can define the preprocessor macro
  2702. `MP_NOCPLUSPLUS' before the inclusion of that file.
  2703.  
  2704.   6. Function where call to `malloc()' took place.  This information is
  2705.      only available if the source file containing the call to
  2706.      `malloc()' was compiled with `gcc' or `g++'.
  2707.  
  2708.   7. Filename in which call to `malloc()' took place.
  2709.  
  2710.   8. Line number at which call to `malloc()' took place.
  2711.  
  2712.    The following information contains function call stack details of
  2713. where the call to `malloc()' came from, but is only available if the
  2714. mpatrol library has been built on a platform that supports this.  The
  2715. top-most entry should be the function which called `malloc()' and the
  2716. bottom-most entry should be the entry-point for the process.
  2717.  
  2718.   9. Address of function call.  This is normally the address of the
  2719.      machine instruction immediately after the function call
  2720.      instruction, also known as the return address.
  2721.  
  2722.  10. Function where call took place.  This information is only
  2723.      available if the mpatrol library has been built on a platform that
  2724.      supports reading symbol table information from executable files,
  2725.      and then only if there is an entry in the symbol table
  2726.      corresponding to the return address.  C++ function names may still
  2727.      be in their mangled form, but this can be easily rectified by
  2728.      processing the log file with a C++ name demangler.
  2729.  
  2730.    The following information is only available when the allocation type
  2731. is `ALLOC' or `REALLOC' since it makes no sense when applied to `FREE'.
  2732.  
  2733.  11. The address of the new memory block that has been allocated by
  2734.      `malloc()'.
  2735.  
  2736.    As you can see, there is quite a lot of information that can be
  2737. displayed from a simple call to `malloc()', and hopefully this
  2738. information has been presented in a clear and concise format in the log
  2739. file.
  2740.  
  2741.    The next entries in the log file correspond to the call to `free()',
  2742. which attempts to free the memory allocated by `malloc()', but supplies
  2743. the wrong address.
  2744.  
  2745.    The first three lines should be self-explanatory as they are very
  2746. similar to those described above for `malloc()'.  However, the next
  2747. lines signal that a terminal error has occurred in the program, so I've
  2748. annotated them as before.
  2749.  
  2750.      FREE: free (0x00028001) [main|test1.c|37]
  2751.              0x00010B24 main
  2752.              0x000109D4 _start
  2753.      
  2754.       (1)   (2)
  2755.        |     |
  2756.        V     V
  2757.      ERROR: free: 0x00028001 does not match allocation of 0x00028000
  2758.             (3)        (4)       (5)  (6)(7)  (8)    (9)  (10)
  2759.              |          |         |    |  |    |      |    |
  2760.              V          V         V    V  V    V      V    V
  2761.          0x00028000 (16 bytes) {malloc:13:0} [main|test1.c|36]
  2762.      (11) -> 0x00010AE0 main
  2763.              0x000109D4 _start
  2764.  
  2765.   1. Error severity.  The mpatrol library has two different severities
  2766.      of error: `WARNING' and `ERROR'.  The first is always recoverable,
  2767.      and serves only to indicate that something is not quite right, and
  2768.      so may be useful in determining where something started to go
  2769.      wrong.  The second may or may not be recoverable, and the library
  2770.      terminates the program if it is fatal, displaying any relevant
  2771.      information as it does this.
  2772.  
  2773.   2. Allocation function.  This is the name of the function used to
  2774.      allocate, reallocate or free memory where the error was detected.
  2775.      This may be omitted if an error is detected elsewhere in the
  2776.      library.
  2777.  
  2778.    The following information is related to the information that the
  2779. library has stored about the relevant memory allocation.  This
  2780. information is always displayed in this format when details of
  2781. individual memory allocations are required.  If any information is
  2782. missing then it simply means that the library was not able to determine
  2783. it when the memory block was first allocated.
  2784.  
  2785.   3. Address of memory allocation.
  2786.  
  2787.   4. Size of memory allocation.
  2788.  
  2789.   5. Allocation function.  This is the name of the function that was
  2790.      called to allocate the memory block, in this case `malloc'.  If
  2791.      the memory allocation has been resized then this will be either
  2792.      `realloc', `recalloc' or `expand'.
  2793.  
  2794.   6. Allocation index.
  2795.  
  2796.   7. Reallocation index.  This is used to count the number of times a
  2797.      memory allocation has been resized with `realloc()', `recalloc()'
  2798.      or `expand()'.
  2799.  
  2800.   8. Function where original call to `malloc()' took place.  If the
  2801.      memory allocation has been resized then this will be the name of
  2802.      the function which last called `realloc()', `recalloc()' or
  2803.      `expand()'.
  2804.  
  2805.   9. Filename in which original call to `malloc()' took place.  If the
  2806.      memory allocation has been resized then this will be the filename
  2807.      in which the last call to `realloc()', `recalloc()' or `expand()'
  2808.      took place.
  2809.  
  2810.  10. Line number at which original call to `malloc()' took place.  If
  2811.      the memory allocation has been resized then this will be the line
  2812.      number at which the last call to `realloc()', `recalloc()' or
  2813.      `expand()' took place.
  2814.  
  2815.  11. Function call stack of original memory allocation.  If the memory
  2816.      allocation has been resized then this will be the call stack of
  2817.      the last call to `realloc()', `recalloc()' or `expand()'.
  2818.  
  2819.    So, the mpatrol library detected the error in the above program and
  2820. terminated it.  When the library terminates it always displays a
  2821. summary of various memory allocation statistics and settings that were
  2822. used during the execution of the program.
  2823.  
  2824.    The various settings and statistics displayed by the library for the
  2825. above example have been numbered and their descriptions appear below.
  2826.  
  2827.       1  system page size:  8192 bytes
  2828.       2  default alignment: 8 bytes
  2829.       3  overflow size:     0 bytes
  2830.       4  overflow byte:     0xAA
  2831.       5  allocation byte:   0xFF
  2832.       6  free byte:         0x55
  2833.       7  allocation stop:   0
  2834.       8  reallocation stop: 0
  2835.       9  free stop:         0
  2836.      10  unfreed abort:     0
  2837.      11  small boundary:    32
  2838.      12  medium boundary:   256
  2839.      13  large boundary:    2048
  2840.      14  lower check range: -
  2841.      15  upper check range: -
  2842.      16  failure frequency: 0
  2843.      17  failure seed:      533453
  2844.      18  prologue function: <unset>
  2845.      19  epilogue function: <unset>
  2846.      20  handler function:  <unset>
  2847.      21  log file:          mpatrol.log
  2848.      22  profiling file:    mpatrol.out
  2849.      23  program filename:  ./test1
  2850.      24  symbols read:      3240
  2851.      25  autosave count:    0
  2852.      26  allocation count:  13
  2853.      27  allocation peak:   4720 bytes
  2854.      28  allocation limit:  0 bytes
  2855.      29  allocated blocks:  1 (16 bytes)
  2856.      30  freed blocks:      0 (0 bytes)
  2857.      31  free blocks:       1 (8176 bytes)
  2858.      32  internal blocks:   25 (204800 bytes)
  2859.      33  total heap usage:  212992 bytes
  2860.      34  total compared:    0 bytes
  2861.      35  total located:     0 bytes
  2862.      36  total copied:      0 bytes
  2863.      37  total set:         0 bytes
  2864.      38  total warnings:    0
  2865.      39  total errors:      1
  2866.  
  2867.   1. System page size.  This value is used on some platforms when
  2868.      allocating and protecting system memory.
  2869.  
  2870.   2. Default alignment.  This value is the minimum alignment required
  2871.      for general purpose memory allocations, and is usually the
  2872.      alignment required by the most restrictive datatype on a given
  2873.      system.  It is used when allocating memory that has no specified
  2874.      alignment.  It can be changed at run-time using the `DEFALIGN'
  2875.      option, but setting this value too small may cause the program to
  2876.      crash due to bus errors which are caused by reading from or
  2877.      writing to misaligned data.
  2878.  
  2879.   3. Overflow size.  This value is the size used by one overflow
  2880.      buffer.  If this is non-zero then every memory allocation will
  2881.      have two overflow buffers; one on either side.  These buffers are
  2882.      used by the library to detect if the program has written too many
  2883.      bytes to a memory allocation, thus overflowing into one of the
  2884.      buffers, but these extra checks can slow down execution speed.  It
  2885.      can be changed at run-time using the `OFLOWSIZE' option.
  2886.  
  2887.   4. Overflow byte.
  2888.  
  2889.   5. Allocation byte.
  2890.  
  2891.   6. Free byte.  These values are used by the library to pre-fill
  2892.      blocks of memory for checking purposes.  The overflow byte is used
  2893.      to fill overflow buffers, the allocation byte is used to fill
  2894.      newly-allocated memory (except from `calloc()' or `recalloc()'),
  2895.      and the free byte is used to fill free blocks or freed memory
  2896.      allocations.  These can be changed at run-time using the
  2897.      `OFLOWBYTE', `ALLOCBYTE' and `FREEBYTE' options.
  2898.  
  2899.   7. Allocation stop.
  2900.  
  2901.   8. Reallocation stop.
  2902.  
  2903.   9. Free stop.  These values are used by the library to halt the
  2904.      program when run inside a debugger whenever a specified allocation
  2905.      index is allocated, reallocated or freed.  These can be changed at
  2906.      run-time using the `ALLOCSTOP', `REALLOCSTOP' and `FREESTOP'
  2907.      options.
  2908.  
  2909.  10. Unfreed abort.  This value is used when the program terminates and
  2910.      is used by the library to check if there are more than a given
  2911.      number of unfreed memory allocations.  If there are then the
  2912.      library will cause the program to abort with an error.  It can be
  2913.      changed at run-time using the `UNFREEDABORT' option.
  2914.  
  2915.  11. Small boundary.
  2916.  
  2917.  12. Medium boundary.
  2918.  
  2919.  13. Large boundary.  These values are used in memory allocation
  2920.      profiling and specify the boundaries in bytes between small,
  2921.      medium, large and extra large allocations.  These can be changed
  2922.      at run-time using the `SMALLBOUND', `MEDIUMBOUND' and `LARGEBOUND'
  2923.      options.
  2924.  
  2925.  14. Lower check range.
  2926.  
  2927.  15. Upper check range.  These values specify the range of allocation
  2928.      indices through which the library will physically check every area
  2929.      of free memory and every overflow buffer for errors.  A dash
  2930.      specifies that either the lower or upper range is infinite, but if
  2931.      they are both zero then no such checking will ever be performed,
  2932.      thus speeding up execution speed dramatically.  The library
  2933.      defaults to performing checks for every allocation index.  These
  2934.      can be changed at run-time using the `CHECK' option.
  2935.  
  2936.  16. Failure frequency.
  2937.  
  2938.  17. Failure seed.  These values are used to specify if random memory
  2939.      allocation failures should occur during program execution, for the
  2940.      purposes of stress testing a program.  If the failure frequency is
  2941.      zero then no random failures will occur, but if it is greater than
  2942.      zero then the higher the number, the less frequent the failures.
  2943.      The failure seed is used internally by the mpatrol library when
  2944.      generating random numbers.  If it is zero then the seed will be set
  2945.      randomly, but if it is greater than zero then it will be used to
  2946.      generate a predictable sequence of random numbers; i.e. two runs
  2947.      of the same program with the same failure frequencies and the same
  2948.      failure seeds will generate exactly the same sequence of failures.
  2949.  
  2950.  18. Prologue function.
  2951.  
  2952.  19. Epilogue function.
  2953.  
  2954.  20. Handler function.  These values contain addresses or names of
  2955.      functions that have been installed as callback functions for the
  2956.      library.  These functions, if set, will be called from the library
  2957.      at appropriate times during program execution in order to handle
  2958.      specific events.  These can be changed at compile-time using the
  2959.      `__mp_prologue()', `__mp_epilogue()' and `__mp_nomemory()'
  2960.      functions.
  2961.  
  2962.  21. Log file.  Simply contains the name of the file where all mpatrol
  2963.      library diagnostics go to.  It can be changed at run-time using
  2964.      the `LOGFILE' option.
  2965.  
  2966.  22. Profiling file.  Contains the name of the file where all of the
  2967.      mpatrol library memory allocation profiling information goes when
  2968.      the `PROF' option is used.  It can be changed at run-time using
  2969.      the `PROFFILE' option.
  2970.  
  2971.  23. Program filename.  Contains the full pathname to the program's
  2972.      executable file.  This is used by the mpatrol library to read the
  2973.      symbol table in order to provide symbolic information in function
  2974.      call stacks.  It can be changed at run-time using the `PROGFILE'
  2975.      option.
  2976.  
  2977.  24. Symbols read.  This value contains the total number of symbols
  2978.      read from a program's executable file and/or the dynamic linker,
  2979.      if applicable.
  2980.  
  2981.  25. Autosave count.  This value contains the frequency at which the
  2982.      mpatrol library should periodically write the profiling data to
  2983.      the profiling output file.  When the total number of profiled
  2984.      memory allocations and deallocations is a multiple of this number
  2985.      then the current profiling information will be written to the
  2986.      profiling output file.  It can be changed at run-time using the
  2987.      `AUTOSAVE' option.
  2988.  
  2989.  26. Allocation count.  This value contains the total number of memory
  2990.      allocations that were created by the mpatrol library.  This value
  2991.      may be more than expected if the mpatrol library makes any memory
  2992.      allocations during initialisation.
  2993.  
  2994.  27. Allocation peak.  This value contains the peak memory usage set by
  2995.      the program when running.  This value may be more than expected if
  2996.      the mpatrol library makes any memory allocations during
  2997.      initialisation.
  2998.  
  2999.  28. Allocation limit.  This value is used to limit the amount of
  3000.      memory that can be allocated by a program, which can be useful for
  3001.      stress-testing in simulated low memory conditions.  It can be
  3002.      changed at run-time using the `LIMIT' option.
  3003.  
  3004.  29. Allocated blocks.
  3005.  
  3006.  30. Freed blocks.
  3007.  
  3008.  31. Free blocks.  These values contain the total number of allocated,
  3009.      freed and free blocks at the time the summary was produced.  A
  3010.      freed block is an allocated block that has been freed but has not
  3011.      been returned to the free memory list for later allocation.  These
  3012.      values may be different from those expected if the mpatrol library
  3013.      makes any memory allocations during initialisation.
  3014.  
  3015.  32. Internal blocks.  This value contains the total number of memory
  3016.      blocks (of varying sizes) that have been allocated from the system
  3017.      for the mpatrol library to use internally.  These memory blocks
  3018.      will be write-protected on systems that support memory protection
  3019.      in order to prevent the program from corrupting the library's data
  3020.      structures.  This can be overridden at run-time using the
  3021.      `NOPROTECT' option in order to speed up program execution slightly.
  3022.  
  3023.  33. Total heap usage.  This value contains the total amount of system
  3024.      heap memory that has been allocated by the mpatrol library.
  3025.  
  3026.  34. Total compared.
  3027.  
  3028.  35. Total located.
  3029.  
  3030.  36. Total copied.
  3031.  
  3032.  37. Total set.  These values contain the total number of bytes that
  3033.      have been tracked by the mpatrol library in byte comparison
  3034.      operations (such as `memcmp()'), byte location operations (such as
  3035.      `memchr()', byte copy operations (such as `memcpy()') and byte set
  3036.      operations (such as `memset()') respectively.  They do not take
  3037.      into account any other such operations that occur outwith these
  3038.      functions, such as loading and storing from machine instructions.
  3039.  
  3040.  38. Total warnings.
  3041.  
  3042.  39. Total errors.  The library keeps a count of the total number of
  3043.      warnings and errors it has displayed so that you can quickly work
  3044.      out this information at program termination.
  3045.  
  3046.    ---------- Footnotes ----------
  3047.  
  3048.    (1) This information may also be filled in if the `USEDEBUG' option
  3049. is used and supported, and if debugging information about the call to
  3050. `malloc()' is available.
  3051.  
  3052. 11.2 Detecting incorrect reuse of freed memory
  3053. ==============================================
  3054.  
  3055.    The next example uses `tests/fail/test2.c' to illustrate how the
  3056. mpatrol library can detect whereabouts on the heap an address belongs.
  3057.  
  3058.      23  /*
  3059.      24   * Allocates a block of 16 bytes and then immediately frees it.  An
  3060.      25   * attempt is then made to double the size of the original block.
  3061.      26   */
  3062.      
  3063.      
  3064.      29  #include "mpatrol.h"
  3065.      
  3066.      
  3067.      32  int main(void)
  3068.      33  {
  3069.      34      char *p;
  3070.      
  3071.      36      if (p = (char *) malloc(16))
  3072.      37      {
  3073.      38          free(p);
  3074.      39          p = (char *) realloc(p, 32);
  3075.      40      }
  3076.      41      return EXIT_SUCCESS;
  3077.      42  }
  3078.  
  3079.    The relevant excerpts from `mpatrol.log' appear below.  The format
  3080. of the log messages should be familiar to you now.
  3081.  
  3082.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test2.c|36]
  3083.              0x00010B18 main
  3084.              0x00010A0C _start
  3085.      
  3086.      returns 0x00028000
  3087.      
  3088.      FREE: free (0x00028000) [main|test2.c|38]
  3089.              0x00010B54 main
  3090.              0x00010A0C _start
  3091.      
  3092.          0x00028000 (16 bytes) {malloc:13:0} [main|test2.c|36]
  3093.              0x00010B18 main
  3094.              0x00010A0C _start
  3095.      
  3096.      REALLOC: realloc (0x00028000, 32 bytes, 8 bytes) [main|test2.c|39]
  3097.              0x00010B88 main
  3098.              0x00010A0C _start
  3099.      
  3100.      ERROR: realloc: 0x00028000 has not been allocated
  3101.      
  3102.      returns 0x00000000
  3103.  
  3104.    The mpatrol library stores all of its information about allocated
  3105. and free memory in tree structures so that it can quickly determine if
  3106. an address belongs to allocated or free memory, or if it even exists in
  3107. the heap that is managed by mpatrol.  The above example should
  3108. illustrate this since after the allocation had been freed, the library
  3109. recognised this and reported an error.  It was possible for the program
  3110. to continue execution even after that error since mpatrol could recover
  3111. from it and return `NULL'.
  3112.  
  3113.    It is possible for mpatrol to give even more useful diagnostics in
  3114. the above situation by using the `NOFREE' option.  This prevents the
  3115. library from returning any freed allocations to the free memory pool,
  3116. by preserving any information about them and marking them as freed.  If
  3117. you add the `NOFREE' option to the `MPATROL_OPTIONS' environment
  3118. variable you should see the following entries in `mpatrol.log' instead.
  3119.  
  3120.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test2.c|36]
  3121.              0x00010B18 main
  3122.              0x00010A0C _start
  3123.      
  3124.      returns 0x00029DE0
  3125.      
  3126.      FREE: free (0x00029DE0) [main|test2.c|38]
  3127.              0x00010B54 main
  3128.              0x00010A0C _start
  3129.      
  3130.          0x00029DE0 (16 bytes) {malloc:13:0} [main|test2.c|36]
  3131.              0x00010B18 main
  3132.              0x00010A0C _start
  3133.      
  3134.      REALLOC: realloc (0x00029DE0, 32 bytes, 8 bytes) [main|test2.c|39]
  3135.              0x00010B88 main
  3136.              0x00010A0C _start
  3137.      
  3138.      ERROR: realloc: 0x00029DE0 was freed with free
  3139.          0x00029DE0 (16 bytes) {free:13:0} [main|test2.c|38]
  3140.              0x00010B54 main
  3141.              0x00010A0C _start
  3142.      
  3143.      returns 0x00000000
  3144.  
  3145.    Note the extra information reported by `realloc()' since the library
  3146. knows all of the details about the freed memory allocation and when it
  3147. was freed.
  3148.  
  3149.    The `NOFREE' option tends to use up much more system memory than
  3150. normal since it effectively instructs the mpatrol library to allocate
  3151. new memory for every single memory allocation or reallocation.  It can
  3152. also slow down program execution when overflow buffers are used, since
  3153. with each new memory allocation the library needs to check more and
  3154. more overflow buffers every time it is called.  However, it can be
  3155. quite useful for problems such as this one.  The test in
  3156. `tests/fail/test3.c' has a similar situation.
  3157.  
  3158.    Normally, the `NOFREE' option will cause the library to fill all
  3159. freed memory allocations with the free byte.  However, the original
  3160. contents of such allocations can be preserved with the `PRESERVE'
  3161. option.  This could help in situations when you need to determine
  3162. exactly if a program is relying on the contents of freed memory.
  3163.  
  3164. 11.3 Detecting use of free memory
  3165. =================================
  3166.  
  3167.    This next example illustrates how the mpatrol library is able to
  3168. check to see if anything has been written into free memory.  The test
  3169. is located in `tests/fail/test4.c' and simply writes a single byte into
  3170. free memory.
  3171.  
  3172.      23  /*
  3173.      24   * Allocates a block of 16 bytes and then immediately frees it.  A
  3174.      25   * NULL character is written into the middle of the freed memory.
  3175.      26   */
  3176.      
  3177.      
  3178.      29  #include "mpatrol.h"
  3179.      
  3180.      
  3181.      32  int main(void)
  3182.      33  {
  3183.      34      char *p;
  3184.      
  3185.      36      if (p = (char *) malloc(16))
  3186.      37      {
  3187.      38          free(p);
  3188.      39          p[8] = '\0';
  3189.      40      }
  3190.      41      return EXIT_SUCCESS;
  3191.      42  }
  3192.  
  3193.    The following output was produced as part of `mpatrol.log'.  Note
  3194. that this test was run using the same `MPATROL_OPTIONS' settings as the
  3195. last example, but make sure that `PRESERVE' is not set.
  3196.  
  3197.      ERROR: freed allocation 0x00029DE0 has memory corruption at 0x00029DE8
  3198.              0x00029DE8  00555555 55555555                    .UUUUUUU
  3199.      
  3200.          0x00029DE0 (16 bytes) {free:13:0} [main|test4.c|38]
  3201.              0x00010B1C main
  3202.              0x000109D4 _start
  3203.  
  3204.    The library was able to detect that something had been written into
  3205. free memory and could report on the memory allocation that was
  3206. overwritten.  However, these checks are only performed whenever a
  3207. function in the mpatrol library is called.  In the example above, the
  3208. code which wrote into free memory could have been miles away from where
  3209. the library detected the error.
  3210.  
  3211.    On platforms that support memory protection, the library also
  3212. supports the `PAGEALLOC' option.  This option instructs the library to
  3213. force every single memory allocation to have a size which is a multiple
  3214. of the system page size.  Although the library still stores the
  3215. original requested size, it effectively means that no two memory
  3216. allocations occupy the same page of memory.  It can then use page
  3217. protection (which only operates on pages of memory) to protect all free
  3218. memory from being read from or written to, and uses similar features to
  3219. install a page of overflow buffer on either side of the allocation.
  3220.  
  3221.    However, if the requested size for the memory allocation was not a
  3222. multiple of the page size this means that there will still be unused
  3223. space left over in the allocated pages.  This problem is solved by
  3224. turning the unused space into overflow buffers that will be checked in
  3225. the normal way.  The positioning of the allocation within its pages is
  3226. also important.  If you want to check for illegal reads from the
  3227. borders of the memory allocation, unless it fits exactly into its pages
  3228. then there is a chance that a program could illegally read the
  3229. right-most overflow buffer if the allocation was left-aligned, or
  3230. vice-versa.  Two settings therefore exist for the `PAGEALLOC' option:
  3231. `LOWER' and `UPPER'.  They refer to the placement of every memory
  3232. allocation within its constituent pages.
  3233.  
  3234.    The following diagram illustrates the `PAGEALLOC' option.  In the
  3235. diagram, the system page size is assumed to be 16 bytes (very unlikely,
  3236. but will serve for this example) and each character represents 1 byte.
  3237.  
  3238.      x = allocated memory
  3239.      o = overflow buffer (filled with the overflow byte)
  3240.      . = overflow buffer page (read and write protected)
  3241.      
  3242.      PAGEALLOC=LOWER, allocation size is 16 bytes or
  3243.      PAGEALLOC=UPPER, allocation size is 16 bytes:
  3244.          ................xxxxxxxxxxxxxxxx................
  3245.      
  3246.      PAGEALLOC=LOWER, allocation size is 8 bytes:
  3247.          ................xxxxxxxxoooooooo................
  3248.      
  3249.      PAGEALLOC=UPPER, allocation size is 8 bytes:
  3250.          ................ooooooooxxxxxxxx................
  3251.  
  3252.    In our original example, if the `PAGEALLOC=LOWER' option is added to
  3253. the `MPATROL_OPTIONS' environment variable then the following error
  3254. will be produced instead of the original error.
  3255.  
  3256.      ERROR: illegal memory access at address 0x0009E008
  3257.          0x0009E000 (16 bytes) {free:13:0} [main|test4.c|38]
  3258.              0x00010B1C main
  3259.              0x000109D4 _start
  3260.      
  3261.          call stack
  3262.              0x00010B1C main
  3263.              0x000109D4 _start
  3264.  
  3265.    On systems that support memory protection, the mpatrol library has a
  3266. built-in signal handler which catches illegal memory accesses and
  3267. terminates the program.  In the above case, the freed memory was made
  3268. write-protected and so could not be written to.  The underlying virtual
  3269. memory system in the operating system noticed this and signaled this to
  3270. the library immediately after it happened.
  3271.  
  3272.    Along with the details of the freed memory allocation that was being
  3273. written to, the library also attempts to display the function call
  3274. stack for the location in the program that caused the illegal memory
  3275. access, although this can be quite unreliable.  A better solution would
  3276. be to run the program in a debugger to catch the illegal memory access.
  3277.  
  3278.    Note that the `PAGEALLOC' option also modifies the behaviour of the
  3279. `NOFREE' and `PRESERVE' options when used together.  The memory
  3280. allocation being freed will always be made write-protected when the
  3281. `PRESERVE' option is used, otherwise it will also be made read-protected
  3282. to prevent further accesses.
  3283.  
  3284.    Note also that the `PAGEALLOC=UPPER' option is potentially much less
  3285. efficient at catching illegal memory accesses than the `PAGEALLOC=LOWER'
  3286. option.  This is due to alignment requirements, since an allocation of
  3287. 1 byte requiring an alignment of 16 bytes cannot be placed at the very
  3288. end of a page of size 4096 bytes.  The following diagram illustrates
  3289. this, using the same page size as the last diagram.
  3290.  
  3291.      x = allocated memory
  3292.      o = overflow buffer (filled with the overflow byte)
  3293.      . = overflow buffer page (read and write protected)
  3294.      
  3295.      PAGEALLOC=UPPER, allocation size is 16 bytes, alignment is 8 bytes:
  3296.          ................xxxxxxxxxxxxxxxx................
  3297.      
  3298.      PAGEALLOC=UPPER, allocation size is 3 bytes, alignment is 1 byte:
  3299.          ................oooooooooooooxxx................
  3300.      
  3301.      PAGEALLOC=UPPER, allocation size is 3 bytes, alignment is 8 bytes:
  3302.          ................ooooooooxxxooooo................
  3303.  
  3304.    Everything is OK until the last allocation, where the alignment
  3305. requirement means that there must be two overflow buffers.  This slows
  3306. down program execution since the library must check an additional
  3307. overflow buffer, and also means that the program would have to read six
  3308. bytes beyond the end of the allocation before the illegal memory access
  3309. would be detected.
  3310.  
  3311. 11.4 Using overflow buffers
  3312. ===========================
  3313.  
  3314.    This example illustrates the use of overflow buffers and so the
  3315. `MPATROL_OPTIONS' environment variable should have `OFLOWSIZE=2' added
  3316. to it.  However, turn off any `PAGEALLOC' options for the purposes of
  3317. this example.  The test is located in `tests/fail/test5.c', and
  3318. `tests/fail/test6.c' is very similar.
  3319.  
  3320.      23  /*
  3321.      24   * Allocates a block of 16 bytes and then copies a string of 16
  3322.      25   * bytes into the block.  However, the string is copied to 1 byte
  3323.      26   * before the allocated block which writes before the start of the
  3324.      27   * block.  This test must be run with an OFLOWSIZE greater than 0.
  3325.      28   */
  3326.      
  3327.      
  3328.      31  #include "mpatrol.h"
  3329.      
  3330.      
  3331.      34  int main(void)
  3332.      35  {
  3333.      36      char *p;
  3334.      
  3335.      38      if (p = (char *) malloc(16))
  3336.      39      {
  3337.      40          strcpy(p - 1, "this test fails!");
  3338.      41          free(p);
  3339.      42      }
  3340.      43      return EXIT_SUCCESS;
  3341.      44  }
  3342.  
  3343.    The following error should be produced in `mpatrol.log'.
  3344.  
  3345.      ERROR: allocation 0x00029E28 has a corrupted overflow buffer at 0x00029E27
  3346.              0x00029E26  AA74                                 ªt
  3347.      
  3348.          0x00029E28 (16 bytes) {malloc:13:0} [main|test5.c|38]
  3349.              0x00010B0C main
  3350.              0x00010A00 _start
  3351.  
  3352.    Once again, the library attempts to show you as much detail as
  3353. possible about where the corruption occurred.  Along with showing you a
  3354. memory dump of the overflow buffer that was corrupted, it also shows
  3355. you the allocation to which the overflow buffer belongs.
  3356.  
  3357.    Using overflow buffers can reduce the speed of program execution
  3358. since the library has to check every buffer whenever it is called, and
  3359. if the buffers are larger then they'll take longer to check and will
  3360. use up more memory.  However, larger buffers mean that there is less
  3361. chance of the program writing past one memory allocation into another.
  3362.  
  3363.    Alternatively, the `CHECK' option can be used to limit the number of
  3364. checks that the library has to perform, thus speeding up program
  3365. execution.  This option specifies a range of allocation indices through
  3366. which the library will check overflow buffers and free memory for
  3367. corruption.  Such checks occur when they normally would, but only if
  3368. the current allocation index falls within the specified range.  This
  3369. feature can be used when there is a suspicion that free memory
  3370. corruption or overflow buffer corruption occurs at a certain point
  3371. during program execution, but checking them at every library call would
  3372. take too long.
  3373.  
  3374.    On systems which support software watch points, there is an extra
  3375. option called `OFLOWWATCH' which allows additional memory protection.
  3376. Watch points allow individual bytes to be read and/or write protected
  3377. as opposed to just pages.  The `OFLOWWATCH' option installs software
  3378. watch points at every overflow buffer instead of requiring the library
  3379. to check the integrity of the overflow buffers, and can be used in
  3380. combination with `PAGEALLOC'.  However, software watch points slow down
  3381. program execution to a crawl since every machine instruction must be
  3382. checked individually by the system to see if it accesses a watch point
  3383. area.  Slowing the program down by a factor of 10,000 is not uncommon
  3384. on some systems when the `OFLOWWATCH' option is used.
  3385.  
  3386. 11.5 Bad memory operations
  3387. ==========================
  3388.  
  3389.    In C there are several basic memory operation functions that are
  3390. often called to perform tasks such as clearing memory, copying memory,
  3391. etc.  The mpatrol library contains replacements for these which allow
  3392. for better checking of their arguments to prevent reading and writing
  3393. past the boundaries of existing memory allocations.  The following
  3394. source can be found in `tests/fail/test9.c'.
  3395.  
  3396.      23  /*
  3397.      24   * Allocates a block of 16 bytes and then attempts to zero the contents of
  3398.      25   * the block.  However, a zero byte is also written 1 byte before and 1
  3399.      26   * byte after the allocated block, resulting in an error in the log file.
  3400.      27   */
  3401.      
  3402.      
  3403.      30  #include "mpatrol.h"
  3404.      
  3405.      
  3406.      33  int main(void)
  3407.      34  {
  3408.      35      char *p;
  3409.      
  3410.      37      if (p = (char *) malloc(16))
  3411.      38      {
  3412.      39          memset(p - 1, 0, 18);
  3413.      40          free(p);
  3414.      41      }
  3415.      42      return EXIT_SUCCESS;
  3416.      43  }
  3417.  
  3418.    When this is compiled and run, the following should appear in the
  3419. log file.
  3420.  
  3421.      ERROR: memset: range [0x00027FFF,0x00028010] overflows [0x00028000,0x0002800F]
  3422.          0x00028000 (16 bytes) {malloc:13:0} [main|test9.c|37]
  3423.              0x00010B18 main
  3424.              0x00010A0C _start
  3425.  
  3426.    As you can see, the library detected that the `memset()' function
  3427. would have written past the boundaries of the memory allocation and
  3428. reported this to you.  It then proceeded to ignore the request to copy
  3429. the memory and continued with the execution of the program(1).  Note
  3430. that this will only be done for known memory allocations.  Reading and
  3431. writing past the boundaries of static and stack memory allocations
  3432. cannot be detected in this way.
  3433.  
  3434.    If the `LOGMEMORY' option is added to the `MPATROL_OPTIONS'
  3435. environment variable then it is possible to see a log of all the
  3436. mpatrol library memory operation functions that were called during
  3437. program execution.  For example, adding this option and running the
  3438. above program again will produce something similar to the following.
  3439.  
  3440.      MEMSET: memset (0x00027FFF, 18 bytes, 0x00) [main|test9.c|39]
  3441.              0x00010B18 main
  3442.              0x00010A0C _start
  3443.  
  3444.    This is similar to the tracing produced for memory allocation
  3445. functions, except that the arguments in parentheses mean different
  3446. things.  For `MEMSET', the first argument represents the start of the
  3447. memory block to set, the second argument represents the number of bytes
  3448. to set and the third argument represents the actual byte to set.
  3449.  
  3450.    For `MEMCOPY', the first argument represents the source memory
  3451. block, the second argument represents the destination memory block, the
  3452. third argument represents the number of bytes to copy and the fourth
  3453. argument represents a byte to copy up to if `memccpy()' is being
  3454. called.  This is similar for `MEMCMP'.
  3455.  
  3456.    For `MEMFIND', the first and second arguments represent the source
  3457. memory block and its length, while the third and fourth arguments
  3458. represent the memory block to search for and its length.  In the
  3459. implementation for `memchr()', the byte to search for is copied to a
  3460. one byte buffer and the address of that buffer is used as the memory
  3461. block to search for.
  3462.  
  3463.    Note that as with the memory allocation functions, `MEMCMP',
  3464. `MEMFIND', `MEMCOPY' and `MEMSET' are used to generalise the types of
  3465. operations being performed and are followed by the names of the actual
  3466. functions being used.  In some cases the functions may use a different
  3467. ordering of parameters than that shown.
  3468.  
  3469.    ---------- Footnotes ----------
  3470.  
  3471.    (1) The error can be turned into a warning with the `ALLOWOFLOW'
  3472. option which will also force the operation to be performed.
  3473.  
  3474. 11.6 Incompatible function calls
  3475. ================================
  3476.  
  3477.    This example illustrates how the mpatrol library checks for calls to
  3478. incompatible pairs of memory allocation functions.  It requires the use
  3479. of C++, although does not use any C++ features except for overloaded
  3480. operators.  The source is in `tests/fail/test7.c', and
  3481. `tests/fail/test8.c' is similar.
  3482.  
  3483.      23  /*
  3484.      24   * Allocates a block of 16 bytes using C++ operator new[] and then
  3485.      25   * attempts to free it using C++ operator delete.
  3486.      26   */
  3487.      
  3488.      
  3489.      29  #include "mpatrol.h"
  3490.      
  3491.      
  3492.      32  int main(void)
  3493.      33  {
  3494.      34      char *p;
  3495.      
  3496.      36      p = new char[16];
  3497.      37      delete p;
  3498.      38      return EXIT_SUCCESS;
  3499.      39  }
  3500.  
  3501.    The relevant parts of `mpatrol.log' are shown below.
  3502.  
  3503.      ALLOC: operator new[] (17, 16 bytes, 8 bytes) [int main()|test7.c|36]
  3504.              0x00010A28 __builtin_vec_new
  3505.              0x00010ADC main
  3506.              0x000108D0 _start
  3507.      
  3508.      returns 0x00028000
  3509.      
  3510.      FREE: operator delete (0x00028000) [int main()|test7.c|37]
  3511.              0x00010A74 __builtin_delete
  3512.              0x00010AF0 main
  3513.              0x000108D0 _start
  3514.      
  3515.      ERROR: operator delete: 0x00028000 was allocated with operator new[]
  3516.          0x00028000 (16 bytes) {operator new[]:17:0} [int main()|test7.c|36]
  3517.              0x00010A28 __builtin_vec_new
  3518.              0x00010ADC main
  3519.              0x000108D0 _start
  3520.  
  3521.    This shows a call to `operator new[]', closely followed by a call to
  3522. `operator delete'.  However, in C++ calls to `operator new[]' must be
  3523. matched by calls to `operator delete[]' and not `operator delete'.
  3524. Hence, the library reports this as an error and does not free the memory
  3525. allocation.
  3526.  
  3527. 11.7 Additional useful information
  3528. ==================================
  3529.  
  3530.    This last example illustrates the various `SHOW' options that are
  3531. available for displaying additional information from the mpatrol
  3532. library at program termination.  It also shows how to easily detect
  3533. memory leaks.  Use the `OFLOWSIZE=16', `NOFREE' and `SHOWALL' options in
  3534. `MPATROL_OPTIONS' before running.
  3535.  
  3536.       1  /*
  3537.       2   * Introduces a memory leak by clobbering a pointer with a new
  3538.       3   * memory allocation.  Use with SHOWUNFREED to display it.
  3539.       4   */
  3540.      
  3541.      
  3542.       7  #include "mpatrol.h"
  3543.      
  3544.      
  3545.      10  int main(void)
  3546.      11  {
  3547.      12      void *p;
  3548.      
  3549.      14      p = malloc(4);
  3550.      15      p = malloc(4);
  3551.      16      if (p != NULL)
  3552.      17          free(p);
  3553.      18      return EXIT_SUCCESS;
  3554.      19  }
  3555.  
  3556.    The information that we are interested in comes after the summary of
  3557. library statistics generated in the log file.  The first block of data
  3558. shows a memory map of the heap that is being handled by mpatrol.  This
  3559. can be used to see graphically where a particular allocation is
  3560. located, or to look for memory fragmentation.  The `SHOWMAP' option
  3561. also displays this information.
  3562.  
  3563.    Note that gaps in the memory map can either be due to space used by
  3564. internal memory blocks or to some other memory allocation library using
  3565. up space.  On some systems that don't have virtual memory, gaps are
  3566. likely to be owned by other processes or belong to the system free
  3567. memory list.
  3568.  
  3569.      memory map:
  3570.        / 0x8000A000-0x8000A00F overflow (16 bytes)
  3571.       |+ 0x8000A010-0x8000A077 allocated (104 bytes) {malloc:1:0} [-|-|-]
  3572.        \ 0x8000A078-0x8000A087 overflow (16 bytes)
  3573.        / 0x8000A088-0x8000A097 overflow (16 bytes)
  3574.       |+ 0x8000A098-0x8000A115 freed (126 bytes) {free:2:0} [-|-|-]
  3575.        \ 0x8000A116-0x8000A125 overflow (16 bytes)
  3576.        / 0x8000A126-0x8000A135 overflow (16 bytes)
  3577.       |+ 0x8000A136-0x8000AF05 freed (3536 bytes) {free:3:0} [-|-|-]
  3578.        \ 0x8000AF06-0x8000AF15 overflow (16 bytes)
  3579.        / 0x8000AF16-0x8000AF25 overflow (16 bytes)
  3580.       |+ 0x8000AF26-0x8000AFA3 freed (126 bytes) {free:4:0} [-|-|-]
  3581.        \ 0x8000AFA4-0x8000AFB3 overflow (16 bytes)
  3582.        / 0x8000AFB4-0x8000AFC3 overflow (16 bytes)
  3583.       |+ 0x8000AFC4-0x8000AFC7 allocated (4 bytes) {malloc:10:0} [main|test.c|14]
  3584.        \ 0x8000AFC8-0x8000AFD7 overflow (16 bytes)
  3585.        / 0x8000AFD8-0x8000AFE7 overflow (16 bytes)
  3586.       |+ 0x8000AFE8-0x8000AFEB freed (4 bytes) {free:11:0} [main|test.c|17]
  3587.        \ 0x8000AFEC-0x8000AFFB overflow (16 bytes)
  3588.      --- 0x8000AFFC-0x8000AFFF free (4 bytes)
  3589.          --------------------- gap (12288 bytes)
  3590.        / 0x8000E000-0x8000E00F overflow (16 bytes)
  3591.       |+ 0x8000E010-0x8000EA27 freed (2584 bytes) {free:5:0} [-|-|-]
  3592.        \ 0x8000EA28-0x8000EA37 overflow (16 bytes)
  3593.        / 0x8000EA38-0x8000EA47 overflow (16 bytes)
  3594.       |+ 0x8000EA48-0x8000EAC5 freed (126 bytes) {free:6:0} [-|-|-]
  3595.        \ 0x8000EAC6-0x8000EAD5 overflow (16 bytes)
  3596.        / 0x8000EAD6-0x8000EAE5 overflow (16 bytes)
  3597.       |+ 0x8000EAE6-0x8000EB63 freed (126 bytes) {free:8:0} [-|-|-]
  3598.        \ 0x8000EB64-0x8000EB73 overflow (16 bytes)
  3599.      --- 0x8000EB74-0x8000EFFF free (1164 bytes)
  3600.          --------------------- gap (8192 bytes)
  3601.        / 0x80011000-0x8001100F overflow (16 bytes)
  3602.       |+ 0x80011010-0x800127F7 freed (6120 bytes) {free:7:0} [-|-|-]
  3603.        \ 0x800127F8-0x80012807 overflow (16 bytes)
  3604.      --- 0x80012808-0x80012FFF free (2040 bytes)
  3605.          --------------------- gap (106496 bytes)
  3606.        / 0x8002D000-0x8002D00F overflow (16 bytes)
  3607.       |+ 0x8002D010-0x8002DBBF freed (2992 bytes) {free:9:0} [-|-|-]
  3608.        \ 0x8002DBC0-0x8002DBCF overflow (16 bytes)
  3609.      --- 0x8002DBD0-0x8002DFFF free (1072 bytes)
  3610.  
  3611.    The next block of data shows a summary of all the symbols that could
  3612. be read from the program's executable file and/or any shared libraries
  3613. that the program requires.  This can be useful to see which symbols
  3614. have actually been read by the mpatrol library.  The `SHOWSYMBOLS'
  3615. option also displays this information.
  3616.  
  3617.    Note that the following data has been dramatically cut down in size
  3618. for the purposes of this example.  The `...' marks text that has been
  3619. removed.
  3620.  
  3621.      symbols read: 2438
  3622.          0x8000076C-0x800007D9 _init [./a.out] (110 bytes)
  3623.          0x80000900-0x8000094F _start [./a.out] (80 bytes)
  3624.          0x80000950-0x8000096F __do_global_dtors_aux [./a.out] (32 bytes)
  3625.          0x80000970-0x80000977 fini_dummy [./a.out] (8 bytes)
  3626.          ...
  3627.          0x80003B24-0x80003B4B __clear_cache [./a.out] (40 bytes)
  3628.          0x80003B4C-0x80003B6F __do_global_ctors_aux [./a.out] (36 bytes)
  3629.          0x80003B70-0x80003B77 init_dummy [./a.out] (8 bytes)
  3630.          0x80003B78-0x80003BA9 _fini [./a.out] (50 bytes)
  3631.          0xC0002604-0xC0002609 _start [/lib/ld.so.1] (6 bytes)
  3632.          0xC000260A-0xC0002659 _dl_start_user [/lib/ld.so.1] (80 bytes)
  3633.          0xC000265A-0xC0002B1B _dl_start [/lib/ld.so.1] (1218 bytes)
  3634.                     0xC000266A here [/lib/ld.so.1] (0 bytes)
  3635.          ...
  3636.          0xC0007A78-0xC0007AB5 __libc_read [/lib/ld.so.1] (62 bytes)
  3637.                     0xC0007A78 read [/lib/ld.so.1] (0 bytes)
  3638.                     0xC0007A9A __syscall_error [/lib/ld.so.1] (0 bytes)
  3639.          0xC0007AB8-0xC0007ADF __clear_cache [/lib/ld.so.1] (40 bytes)
  3640.          0xC0013E70-0xC0013E8B __mp_newlist [/usr/lib/libmpatrol.so.1.0] (28 bytes)
  3641.          0xC0013E8C-0xC0013EB3 __mp_addhead [/usr/lib/libmpatrol.so.1.0] (40 bytes)
  3642.          0xC0013EB4-0xC0013EE7 __mp_addtail [/usr/lib/libmpatrol.so.1.0] (52 bytes)
  3643.          0xC0013EE8-0xC0013F1B __mp_prepend [/usr/lib/libmpatrol.so.1.0] (52 bytes)
  3644.          ...
  3645.          0xC001A0DC-0xC001A0FF __nw__FUi [/usr/lib/libmpatrol.so.1.0] (36 bytes)
  3646.          0xC001A100-0xC001A123 __arr_nw__FUi [/usr/lib/libmpatrol.so.1.0] (36 bytes)
  3647.          0xC001A124-0xC001A143 __dl__FPv [/usr/lib/libmpatrol.so.1.0] (32 bytes)
  3648.          0xC001A144-0xC001A163 __arr_dl__FPv [/usr/lib/libmpatrol.so.1.0] (32 bytes)
  3649.          0xC003BB14-0xC003BB45 __libc_global_ctors [/lib/libc.so.6] (50 bytes)
  3650.          0xC003BB48-0xC003BB97 __libc_init [/lib/libc.so.6] (80 bytes)
  3651.          0xC003BB98-0xC003BBC3 __libc_print_version [/lib/libc.so.6] (44 bytes)
  3652.          0xC003BBC4-0xC003BBD7 __libc_main [/lib/libc.so.6] (20 bytes)
  3653.          ...
  3654.          0xC008F8BC-0xC008FA4D __moddi3 [/lib/libc.so.6] (402 bytes)
  3655.          0xC008FA50-0xC008FB19 __udivdi3 [/lib/libc.so.6] (202 bytes)
  3656.          0xC008FB1C-0xC008FC1B __umoddi3 [/lib/libc.so.6] (256 bytes)
  3657.          0xC008FC1C-0xC008FC4D _fini [/lib/libc.so.6] (50 bytes)
  3658.  
  3659.    The next block of data shows a summary of all freed memory
  3660. allocations.  This is only possible because the `NOFREE' option was
  3661. also given, otherwise there would be no details on freed memory
  3662. allocations.  All of these entries show where the allocation was freed,
  3663. which can be useful if you quickly needed to see where an allocation
  3664. was freed.  The `SHOWFREED' option also displays this information.
  3665.  
  3666.    As this example was run on UNIX, the mpatrol library replaces the
  3667. default implementations of `malloc()', `free()', etc.  As can be seen
  3668. below, this allows the library to trace all calls to allocate dynamic
  3669. memory in a process, even from functions that were not compiled with
  3670. mpatrol.  The two functions shown below were called by the mpatrol
  3671. library in order to read the symbols from ELF object files.  However,
  3672. they are located in the ELF access library which was not compiled with
  3673. mpatrol.
  3674.  
  3675.    Note that the following data has again been cut down in size for the
  3676. purposes of this example.  The `...' marks text that has been removed.
  3677.  
  3678.      freed allocations: 9 (15740 bytes)
  3679.          0x8000A098 (126 bytes) {free:2:0} [-|-|-]
  3680.              0x800011BC elf_end
  3681.              0xC0019668 __mp_init
  3682.              0xC001982A __mp_alloc
  3683.              0x8000099C main
  3684.              0x80000944 _start
  3685.      
  3686.          0x8000A136 (3536 bytes) {free:3:0} [-|-|-]
  3687.              0x8000104E _elf_free
  3688.              0xC0019668 __mp_init
  3689.              0xC001982A __mp_alloc
  3690.              0x8000099C main
  3691.              0x80000944 _start
  3692.      
  3693.          ...
  3694.  
  3695.    The final block of data shows a summary of all unfreed memory
  3696. allocations.  This can show up memory leaks, although the first unfreed
  3697. memory allocation in this example comes from the standard C library.
  3698. On systems such as UNIX it does not really matter about these unfreed
  3699. allocations since they will automatically be returned to the system on
  3700. process termination.
  3701.  
  3702.    However, the second unfreed allocation shows an example of a memory
  3703. leak, where no pointers referencing that allocation remain in the
  3704. program to free it with.  If this was within a loop then the program
  3705. could quickly run away with memory, causing at least a decrease in
  3706. performance, and at most a memory shortage.  The mpatrol library makes
  3707. it easier to spot memory leaks.
  3708.  
  3709.    The `SHOWUNFREED' option also displays this information.
  3710.  
  3711.      unfreed allocations: 2 (108 bytes)
  3712.          0x8000A010 (104 bytes) {malloc:1:0} [-|-|-]
  3713.              0xC0052B4A _IO_fopen
  3714.              0xC0017A0C __mp_openlogfile
  3715.              0xC0019648 __mp_init
  3716.              0xC001982A __mp_alloc
  3717.              0x8000099C main
  3718.              0x80000944 _start
  3719.      
  3720.          0x8000AFC4 (4 bytes) {malloc:10:0} [main|test.c|14]
  3721.              0x8000099C main
  3722.              0x80000944 _start
  3723.  
  3724. 12 Tutorial
  3725. ***********
  3726.  
  3727.    In this chapter we'll look at a real example of using the mpatrol
  3728. library to debug a program.  All of the following building and
  3729. debugging steps were performed on a Linux/m68k machine so the details
  3730. may differ slightly on your system, but the concepts should remain the
  3731. same.  However, on systems without virtual memory some of the steps may
  3732. actually cause the machine to lock up or crash so be aware of this if
  3733. you are running such a system -- you may be safer just reading this
  3734. tutorial rather than attempting it!
  3735.  
  3736.    This tutorial will also make use of the option `USEDEBUG' which
  3737. displays source-level file names and line numbers associated with
  3738. symbols in call stack tracebacks, but only if the underlying object
  3739. file access library supports reading line tables from object files and
  3740. even then only if the object files were compiled with debugging
  3741. information enabled.
  3742.  
  3743.    The program we are going to look at is a simple filter which
  3744. processes its standard input and displays the processed information on
  3745. its standard output.  In this case the program converts all lowercase
  3746. characters to uppercase and removes any blank lines.  The source for
  3747. the program is given below, but can also be found in
  3748. `tests/tutorial/test1.c'.
  3749.  
  3750.      23  /*
  3751.      24   * Reads the standard input file stream, converts all lowercase
  3752.      25   * characters to uppercase, and displays all non-empty lines to the
  3753.      26   * standard output file stream.
  3754.      27   */
  3755.      
  3756.      
  3757.      30  #include <stdio.h>
  3758.      31  #include <stdlib.h>
  3759.      32  #include <string.h>
  3760.      33  #include <ctype.h>
  3761.      
  3762.      
  3763.      36  char *strtoupper(char *s)
  3764.      37  {
  3765.      38      char *t;
  3766.      39      size_t i, l;
  3767.      
  3768.      41      l = strlen(s);
  3769.      42      t = (char *) malloc(l);
  3770.      43      for (i = 0; i < l; i++)
  3771.      44          t[i] = toupper(s[i]);
  3772.      45      t[i] = '\0';
  3773.      46      return t;
  3774.      47  }
  3775.      
  3776.      
  3777.      50  int main(void)
  3778.      51  {
  3779.      52      char *b, *s;
  3780.      
  3781.      54      b = (char *) malloc(BUFSIZ);
  3782.      55      while (gets(b))
  3783.      56      {
  3784.      57          s = strtoupper(b);
  3785.      58          if (*s != '\0')
  3786.      59          {
  3787.      60              puts(s);
  3788.      61              free(s);
  3789.      62          }
  3790.      63      }
  3791.      64      free(b);
  3792.      65      return EXIT_SUCCESS;
  3793.      66  }
  3794.  
  3795.    If you quickly skimmed over the above code then you might have
  3796. noticed some rather obvious errors, but there are also some less
  3797. obvious ones hidden there as well.  After compiling and linking with
  3798. the system C compiler and libraries it successfully runs, even when its
  3799. source code is piped to it.  So if it runs, why bother trying to debug
  3800. it?
  3801.  
  3802.    The short answer to that is that this program does in fact contain
  3803. one rather major error that is likely to prevent it from running
  3804. portably on other systems.  However, for the purposes of this tutorial,
  3805. we'll pretend that we've just been handed the source code for this
  3806. program and have not worked on it before.  So let's now try to compile
  3807. and link it with the mpatrol library(1).
  3808.  
  3809.    First, add the inclusion of `mpatrol.h' to line 34 so that we can
  3810. replace calls to `malloc()' and `free()' with their mpatrol
  3811. equivalents(2).  Then, recompile the program and link it with the
  3812. mpatrol library.  This time, running it with even the simplest of
  3813. non-empty input lines should cause it to abort!
  3814.  
  3815.    If you look at the `mpatrol.log' file produced, you should see
  3816. something along the lines of the following at the end of the log file.
  3817.  
  3818.  
  3819.      ERROR: free memory corruption at 0x8000706C
  3820.              0x8000706C  00555555 55555555 55555555 55555555  .UUUUUUUUUUUUUUU
  3821.              0x8000707C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3822.              0x8000708C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3823.              0x8000709C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3824.              0x800070AC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3825.              0x800070BC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3826.              0x800070CC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3827.              0x800070DC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3828.              0x800070EC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3829.              0x800070FC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3830.              0x8000710C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3831.              0x8000711C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3832.              0x8000712C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3833.              0x8000713C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3834.              0x8000714C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3835.              0x8000715C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3836.  
  3837.    This tells us that something has written a zero byte into free
  3838. memory at location `0x8000706C'.  Unfortunately, the library only
  3839. caught it at the next call to one of its functions so it had already
  3840. happened somewhere in between the last call and the current call.
  3841. Turning on the `LOGALL' option in the `MPATROL_OPTIONS' environment
  3842. variable allows us to see the last successful function call to the
  3843. mpatrol library.
  3844.  
  3845.      ALLOC: malloc (50, 8192 bytes, 2 bytes) [main|test1.c|54]
  3846.              0x80000A30 main (/usr/users/homedir/graeme/test1.c:54)
  3847.              0x80000944 _start
  3848.      
  3849.      returns 0x80009000
  3850.      
  3851.      ALLOC: malloc (51, 4 bytes, 2 bytes) [strtoupper|test1.c|42]
  3852.              0x800009AE strtoupper (/usr/users/homedir/graeme/test1.c:42)
  3853.              0x80000A54 main (/usr/users/homedir/graeme/test1.c:57)
  3854.              0x80000944 _start
  3855.      
  3856.      returns 0x80007068
  3857.  
  3858.    Unfortunately, this only tells us that the last successful mpatrol
  3859. library function call was `malloc()' called from `strtoupper()'.  If we
  3860. add the option `OFLOWSIZE=8' to the `MPATROL_OPTIONS' environment
  3861. variable then we get slightly more information about which memory
  3862. allocation was affected(3).
  3863.  
  3864.      ERROR: allocation 0x80007080 has a corrupted overflow buffer at 0x80007084
  3865.              0x80007084  00AAAAAA AAAAAAAA                    ........
  3866.      
  3867.          0x80007080 (4 bytes) {malloc:51:0} [strtoupper|test1.c|42]
  3868.              0x800009AE strtoupper (/usr/users/homedir/graeme/test1.c:42)
  3869.              0x80000A54 main (/usr/users/homedir/graeme/test1.c:57)
  3870.              0x80000944 _start
  3871.  
  3872.    Now we can make a better guess about what is happening.  Since the
  3873. start of the upper overflow buffer of allocation 51 has been written
  3874. to, we can assume that something has written one byte beyond the end of
  3875. that memory allocation.  You can probably see where that is happening
  3876. now by looking at the code, but let's try to be even more sure that
  3877. this is what is wrong.
  3878.  
  3879.    The only foolproof way to do this is to add a software watch point
  3880. to keep an eye on the address that is being written to.  This can
  3881. normally only be done within a debugger, but on systems that support
  3882. programmable software watch points, the `OFLOWWATCH' option can be used
  3883. to do the same thing.  For the sake of generality, we'll use the
  3884. debugger watch point approach, in this case with `gdb'.  In order for
  3885. the following example to work correctly you'll need to add the
  3886. `ALLOCSTOP=51' option to the `MPATROL_OPTIONS' environment variable so
  3887. that we can stop just after the last successful memory allocation.
  3888.  
  3889.      (gdb) break main
  3890.      Breakpoint 1 at 0x80000a10: file test1.c, line 54.
  3891.      (gdb) run
  3892.      Starting program: a.out
  3893.      Breakpoint 1, main() at test1.c:54
  3894.      54          b = malloc(BUFSIZ);
  3895.      (gdb) break __mp_trap
  3896.      Breakpoint 2 at 0xc00182ac
  3897.      (gdb) continue
  3898.      Continuing.
  3899.      test
  3900.      Breakpoint 2, 0xc00182ac in __mp_trap()
  3901.      (gdb) backtrace
  3902.      #0  0xc00182ac in __mp_trap()
  3903.      #1  0xc0016494 in __mp_getmemory()
  3904.      #2  0xc001a618 in __mp_alloc()
  3905.      #3  0x800009ae in strtoupper(s=0x80009008 "test") at test1.c:42
  3906.      #4  0x80000a54 in main() at test1.c:57
  3907.      (gdb) step
  3908.      Single stepping until exit from function __mp_trap,
  3909.      which has no line number information.
  3910.      0xc0016494 in __mp_getmemory()
  3911.      (gdb) step
  3912.      Single stepping until exit from function __mp_getmemory,
  3913.      which has no line number information.
  3914.      0xc001a618 in __mp_alloc()
  3915.      (gdb) step
  3916.      Single stepping until exit from function __mp_alloc,
  3917.      which has no line number information.
  3918.      strtoupper(s=0x80009008 "test") at test1.c:43
  3919.      43          for (i = 0; i < l; i++)
  3920.      (gdb) watch *0x80007084
  3921.      Watchpoint 3: *2147512452
  3922.      (gdb) continue
  3923.      Continuing.
  3924.      Watchpoint 3: *2147512452
  3925.      Old value = -1431655766
  3926.      New value = 11184810
  3927.      strtoupper(s=0x80009008 "test") at test1.c:46
  3928.      46          return t;
  3929.      (gdb) quit
  3930.      The program is running.  Quit anyway (and kill it)? (y or n) y
  3931.  
  3932.    After loading the program into `gdb', we need to break at `main()'
  3933. so that we can run to a point where all of the shared library symbols
  3934. have been loaded into memory(4).  We can then set another breakpoint at
  3935. `__mp_trap()' and continue until allocation 51 has been reached.
  3936.  
  3937.    Because the mpatrol library has not been built with debugging
  3938. information in this example we can quickly step back to the
  3939. `strtoupper()' function since `gdb' won't step through functions that
  3940. have no debugging information.  We then set a watch point on address
  3941. `0x80007084', which is the address of the memory location that has been
  3942. causing the problems.  After continuing, the debugger stops at line 46,
  3943. but this is more likely to be line 45 since that is where a zero byte
  3944. is being written to(5).
  3945.  
  3946.    So, we have located the problem, which is simply a case of not
  3947. allocating enough memory to contain the copied string _and_ the
  3948. terminating zero byte.  We can also improve the `strtoupper()' function
  3949. by checking the pointer returned by `malloc()' to see if it is `NULL',
  3950. and if so simply exit with an error.  You can try running the program
  3951. with the `FAILFREQ' option to see how it would originally behave in a
  3952. low memory situation.
  3953.  
  3954.    The following listing shows the above modifications that we have
  3955. made to our program.  It can also be found in `tests/tutorial/test2.c'.
  3956.  
  3957.      23  /*
  3958.      24   * Reads the standard input file stream, converts all lowercase
  3959.      25   * characters to uppercase, and displays all non-empty lines to the
  3960.      26   * standard output file stream.
  3961.      27   */
  3962.      
  3963.      
  3964.      30  #include <stdio.h>
  3965.      31  #include <stdlib.h>
  3966.      32  #include <string.h>
  3967.      33  #include <ctype.h>
  3968.      34  #include "mpatrol.h"
  3969.      
  3970.      
  3971.      37  char *strtoupper(char *s)
  3972.      38  {
  3973.      39      char *t;
  3974.      40      size_t i, l;
  3975.      
  3976.      42      l = strlen(s);
  3977.      43      if ((t = (char *) malloc(l + 1)) == NULL)
  3978.      44      {
  3979.      45          fputs("strtoupper: out of memory\n", stderr);
  3980.      46          exit(EXIT_FAILURE);
  3981.      47      }
  3982.      48      for (i = 0; i < l; i++)
  3983.      49          t[i] = toupper(s[i]);
  3984.      50      t[i] = '\0';
  3985.      51      return t;
  3986.      52  }
  3987.      
  3988.      
  3989.      55  int main(void)
  3990.      56  {
  3991.      57      char *b, *s;
  3992.      
  3993.      59      b = (char *) malloc(BUFSIZ);
  3994.      60      while (gets(b))
  3995.      61      {
  3996.      62          s = strtoupper(b);
  3997.      63          if (*s != '\0')
  3998.      64          {
  3999.      65              puts(s);
  4000.      66              free(s);
  4001.      67          }
  4002.      68      }
  4003.      69      free(b);
  4004.      70      return EXIT_SUCCESS;
  4005.      71  }
  4006.  
  4007.    Leaving aside the obvious problem with `gets()' and the general
  4008. inefficiency of the algorithm, we could assume that our program works
  4009. safely now and we can release it to the outside world.  However, a user
  4010. soon reports a problem with our program steadily using more and more
  4011. memory during its execution when processing very large files.
  4012.  
  4013.    This is generally attributable to a memory leak and so we can use the
  4014. `SHOWUNFREED' option to try to detect where the memory leak is coming
  4015. from.  Following is some example output from the mpatrol log file when
  4016. our program is run and is given a relatively small text file as input.
  4017.  
  4018.      unfreed allocations: 6 (109 bytes)
  4019.          0x80007000 (104 bytes) {malloc:1:0} [-|-|-]
  4020.              0xC008DB4A _IO_fopen
  4021.              0xC00183DC __mp_openlogfile
  4022.              0xC001A3A4 __mp_init
  4023.              0xC001A584 __mp_alloc
  4024.              0x80000A98 main
  4025.              0x80000980 _start
  4026.      
  4027.          0x80007068 (1 byte) {malloc:52:0} [strtoupper|test2.c|43]
  4028.              0x800009EE strtoupper
  4029.              0x80000ABC main
  4030.              0x80000980 _start
  4031.      
  4032.          0x8000706A (1 byte) {malloc:54:0} [strtoupper|test2.c|43]
  4033.              0x800009EE strtoupper
  4034.              0x80000ABC main
  4035.              0x80000980 _start
  4036.      
  4037.          0x8000706C (1 byte) {malloc:56:0} [strtoupper|test2.c|43]
  4038.              0x800009EE strtoupper
  4039.              0x80000ABC main
  4040.              0x80000980 _start
  4041.      
  4042.          0x8000706E (1 byte) {malloc:58:0} [strtoupper|test2.c|43]
  4043.              0x800009EE strtoupper
  4044.              0x80000ABC main
  4045.              0x80000980 _start
  4046.      
  4047.          0x80007070 (1 byte) {malloc:60:0} [strtoupper|test2.c|43]
  4048.              0x800009EE strtoupper
  4049.              0x80000ABC main
  4050.              0x80000980 _start
  4051.  
  4052.    We can discount the first entry since that is obviously coming from
  4053. when the mpatrol library first initialises itself.  However, all of the
  4054. other entries appear to be coming from line 43 within `strtoupper()'
  4055. and appear to be only 1 byte in length.  At that point in the code, the
  4056. only possible reason for allocating 1 byte is when the string is empty
  4057. and so that must mean that we are not freeing memory that contains
  4058. empty strings.  Looking at line 66 we can see that `free()' is only
  4059. ever called for non-empty strings and therefore if we move the call to
  4060. `free()' outside the test for an empty string we will fix the memory
  4061. leak.  The file `tests/tutorial/test3.c' contains the source for the
  4062. final program.
  4063.  
  4064.    ---------- Footnotes ----------
  4065.  
  4066.    (1) On UNIX systems with dynamic linking it might also be possible
  4067. to run the program under the mpatrol command with its `-d' option
  4068. without having to recompile or relink, but compiling and linking with
  4069. the mpatrol library is a more generic solution across different
  4070. platforms.
  4071.  
  4072.    (2) This is not strictly necessary on UNIX and Windows platforms
  4073. (and AmigaOS when using `gcc'), but it does give us more debugging
  4074. information.
  4075.  
  4076.    (3) Note that the start address of the allocation has changed
  4077. slightly since we added padding around it with the `OFLOWSIZE' option.
  4078.  
  4079.    (4) This is only necessary when the mpatrol library has been built
  4080. as a shared library.
  4081.  
  4082.    (5) This is not necessarily the fault of the debugger or the
  4083. debugging information generated by the compiler since on most platforms
  4084. such watchpoints can only be caught after they occur, hence most
  4085. debuggers show the next statement to be executed rather than the current
  4086. one.
  4087.  
  4088. Appendix A Functions
  4089. ********************
  4090.  
  4091.    The mpatrol library contains implementations of dynamic memory
  4092. allocation functions for C and C++ suitable for tracing and debugging.
  4093. The library is intended to be used without requiring any changes to
  4094. existing user source code except the inclusion of the `mpatrol.h'
  4095. header file, although additional functions are supplied for extra
  4096. tracing and control.  Note that the current version of the mpatrol
  4097. library is contained in the `MPATROL_VERSION' preprocessor macro.
  4098.  
  4099.    All of the function definitions in `mpatrol.h' can be disabled by
  4100. defining the `NDEBUG' preprocessor macro, which is the same macro used
  4101. to control the behaviour of the `assert()' function.  If `NDEBUG' is
  4102. defined then no macro redefinition of functions will take place and all
  4103. special mpatrol library functions will evaluate to empty statements.
  4104. It is intended that the `NDEBUG' preprocessor macro be defined in
  4105. release builds.
  4106.  
  4107.    The following 14 functions are available as replacements for
  4108. existing C library functions.  To use these you must include
  4109. `mpatrol.h' before all other header files, although on UNIX and Windows
  4110. platforms (and AmigaOS when using `gcc') they will be used anyway,
  4111. albeit with slightly less tracing information.
  4112.  
  4113. `void *malloc(size_t size)'
  4114.      Allocates SIZE uninitialised bytes from the heap and returns a
  4115.      pointer to the first byte of the allocation.  The pointer returned
  4116.      will be suitably aligned for casting to any type and can be used
  4117.      to store data of up to SIZE bytes in length.  If SIZE is `0' then
  4118.      the memory allocated will be implicitly rounded up to `1' byte.
  4119.      If there is not enough space in the heap then the `NULL' pointer
  4120.      will be returned and `errno' will be set to `ENOMEM'.  The
  4121.      allocated memory must be deallocated with `free()' or reallocated
  4122.      with `realloc()'.
  4123.  
  4124. `void *calloc(size_t nelem, size_t size)'
  4125.      Allocates NELEM elements of SIZE zero-initialised bytes from the
  4126.      heap and returns a pointer to the first byte of the allocation.
  4127.      The pointer returned will be suitably aligned for casting to any
  4128.      type and can be used to store data of up to `nelem * size' bytes
  4129.      in length.  If `nelem * size' is `0' then the amount of memory
  4130.      allocated will be implicitly rounded up to `1' byte.  If there is
  4131.      not enough space in the heap then the `NULL' pointer will be
  4132.      returned and `errno' will be set to `ENOMEM'.  The allocated
  4133.      memory must be deallocated with `free()' or reallocated with
  4134.      `realloc()'.
  4135.  
  4136. `void *memalign(size_t align, size_t size)'
  4137.      Allocates SIZE uninitialised bytes from the heap and returns a
  4138.      pointer to the first byte of the allocation.  The pointer returned
  4139.      will be aligned to ALIGN bytes and can be used to store data of up
  4140.      to SIZE bytes in length.  If ALIGN is zero then the default system
  4141.      alignment will be used.  If ALIGN is not a power of two then it
  4142.      will be rounded up to the nearest power of two.  If ALIGN is
  4143.      greater than the system page size then it will be truncated to
  4144.      that value.  If SIZE is `0' then the memory allocated will be
  4145.      implicitly rounded up to `1' byte.  If there is not enough space in
  4146.      the heap then the `NULL' pointer will be returned and `errno' will
  4147.      be set to `ENOMEM'.  The allocated memory must be deallocated with
  4148.      `free()' or reallocated with `realloc()', although the latter will
  4149.      not guarantee the preservation of alignment.
  4150.  
  4151. `void *valloc(size_t size)'
  4152.      Allocates SIZE uninitialised bytes from the heap and returns a
  4153.      pointer to the first byte of the allocation.  The pointer returned
  4154.      will be aligned to the system page size and can be used to store
  4155.      data of up to SIZE bytes in length.  If SIZE is `0' then the
  4156.      memory allocated will be implicitly rounded up to `1' byte.  If
  4157.      there is not enough space in the heap then the `NULL' pointer will
  4158.      be returned and `errno' will be set to `ENOMEM'.  The allocated
  4159.      memory must be deallocated with `free()' or reallocated with
  4160.      `realloc()', although the latter will not guarantee the
  4161.      preservation of alignment.
  4162.  
  4163. `void *pvalloc(size_t size)'
  4164.      Allocates SIZE uninitialised bytes from the heap and returns a
  4165.      pointer to the first byte of the allocation.  The pointer returned
  4166.      will be aligned to the system page size and can be used to store
  4167.      data of up to SIZE bytes in length.  If SIZE is `0' then the
  4168.      memory allocated will be implicitly rounded up to `1' page,
  4169.      otherwise SIZE will be implicitly rounded up to a multiple of the
  4170.      system page size.  If there is not enough space in the heap then
  4171.      the `NULL' pointer will be returned and `errno' will be set to
  4172.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4173.      or reallocated with `realloc()', although the latter will not
  4174.      guarantee the preservation of alignment.
  4175.  
  4176. `char *strdup(const char *str)'
  4177.      Allocates exactly enough memory from the heap to duplicate STR
  4178.      (including the terminating nul character) and returns a pointer to
  4179.      the first byte of the allocation after copying STR to the
  4180.      newly-allocated memory.  The pointer returned will have no
  4181.      alignment constraints and can be used to store character data up
  4182.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4183.      will be returned.  If there is not enough space in the heap then
  4184.      the `NULL' pointer will be returned and `errno' will be set to
  4185.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4186.      or reallocated with `realloc()'.
  4187.  
  4188. `char *strndup(const char *str, size_t size)'
  4189.      Allocates exactly enough memory from the heap to duplicate STR
  4190.      (including the terminating nul character) and returns a pointer to
  4191.      the first byte of the allocation after copying STR to the
  4192.      newly-allocated memory.  The pointer returned will have no
  4193.      alignment constraints and can be used to store character data up
  4194.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4195.      will be returned.  If the length of STR is greater than SIZE then
  4196.      only SIZE characters will be allocated and copied, with one
  4197.      additional byte for the nul character.  If there is not enough
  4198.      space in the heap then the `NULL' pointer will be returned and
  4199.      `errno' will be set to `ENOMEM'.  The allocated memory must be
  4200.      deallocated with `free()' or reallocated with `realloc()'.  This
  4201.      function is available for backwards compatibility with older C
  4202.      libraries and should not be used in new code.
  4203.  
  4204. `char *strsave(const char *str)'
  4205.      Allocates exactly enough memory from the heap to duplicate STR
  4206.      (including the terminating nul character) and returns a pointer to
  4207.      the first byte of the allocation after copying STR to the
  4208.      newly-allocated memory.  The pointer returned will have no
  4209.      alignment constraints and can be used to store character data up
  4210.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4211.      will be returned.  If there is not enough space in the heap then
  4212.      the `NULL' pointer will be returned and `errno' will be set to
  4213.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4214.      or reallocated with `realloc()'.  This function is available for
  4215.      backwards compatibility with older C libraries and should not be
  4216.      used in new code.
  4217.  
  4218. `char *strnsave(const char *str, size_t size)'
  4219.      Allocates exactly enough memory from the heap to duplicate STR
  4220.      (including the terminating nul character) and returns a pointer to
  4221.      the first byte of the allocation after copying STR to the
  4222.      newly-allocated memory.  The pointer returned will have no
  4223.      alignment constraints and can be used to store character data up
  4224.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4225.      will be returned.  If the length of STR is greater than SIZE then
  4226.      only SIZE characters will be allocated and copied, with one
  4227.      additional byte for the nul character.  If there is not enough
  4228.      space in the heap then the `NULL' pointer will be returned and
  4229.      `errno' will be set to `ENOMEM'.  The allocated memory must be
  4230.      deallocated with `free()' or reallocated with `realloc()'.  This
  4231.      function is available for backwards compatibility with older C
  4232.      libraries and should not be used in new code.
  4233.  
  4234. `void *realloc(void *ptr, size_t size)'
  4235.      Resizes the memory allocation beginning at PTR to SIZE bytes and
  4236.      returns a pointer to the first byte of the new allocation after
  4237.      copying PTR to the newly-allocated memory, which will be truncated
  4238.      if SIZE is smaller than the original allocation.  The pointer
  4239.      returned will be suitably aligned for casting to any type and can
  4240.      be used to store data of up to SIZE bytes in length.  If PTR is
  4241.      `NULL' then the call will be equivalent to `malloc()'.  If SIZE is
  4242.      `0' then the existing memory allocation will be freed and the
  4243.      `NULL' pointer will be returned.  If SIZE is greater than the
  4244.      original allocation then the extra space will be filled with
  4245.      uninitialised bytes.  If there is not enough space in the heap
  4246.      then the `NULL' pointer will be returned and `errno' will be set to
  4247.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4248.      and can be reallocated again with `realloc()'.
  4249.  
  4250. `void *recalloc(void *ptr, size_t nelem, size_t size)'
  4251.      Resizes the memory allocation beginning at PTR to NELEM elements of
  4252.      SIZE bytes and returns a pointer to the first byte of the new
  4253.      allocation after copying PTR to the newly-allocated memory, which
  4254.      will be truncated if `nelem * size' is smaller than the original
  4255.      allocation.  The pointer returned will be suitably aligned for
  4256.      casting to any type and can be used to store data of up to `nelem
  4257.      * size' bytes in length.  If PTR is `NULL' then the call will be
  4258.      equivalent to `calloc()'.  If `nelem * size' is `0' then the
  4259.      existing memory allocation will be freed and the `NULL' pointer
  4260.      will be returned.  If `nelem * size' is greater than the original
  4261.      allocation then the extra space will be filled with
  4262.      zero-initialised bytes.  If there is not enough space in the heap
  4263.      then the `NULL' pointer will be returned and `errno' will be set to
  4264.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4265.      and can be reallocated again with `realloc()'.  This function is
  4266.      available for backwards compatibility with older C libraries and
  4267.      `calloc()' and should not be used in new code.
  4268.  
  4269. `void *expand(void *ptr, size_t size)'
  4270.      Attempts to resize the memory allocation beginning at PTR to SIZE
  4271.      bytes and either returns PTR if there was enough space to resize
  4272.      it, or `NULL' if the block could not be resized for a particular
  4273.      reason.  If PTR is `NULL' then the call will be equivalent to
  4274.      `malloc()'.  If SIZE is `0' then the existing memory allocation
  4275.      will be freed and the `NULL' pointer will be returned.  If SIZE is
  4276.      greater than the original allocation then the extra space will be
  4277.      filled with uninitialised bytes and if SIZE is less than the
  4278.      original allocation then the memory block will be truncated.  If
  4279.      there is not enough space in the heap then the `NULL' pointer will
  4280.      be returned and `errno' will be set to `ENOMEM'.  The allocated
  4281.      memory must be deallocated with `free()' and can be reallocated
  4282.      again with `realloc()'.  This function is available for backwards
  4283.      compatibility with older C libraries and should not be used in new
  4284.      code.
  4285.  
  4286. `void free(void *ptr)'
  4287.      Frees the memory allocation beginning at PTR so the memory can be
  4288.      reused by another call to allocate memory.  If PTR is `NULL' then
  4289.      no memory will be freed.  All of the previous contents will be
  4290.      destroyed.
  4291.  
  4292. `void cfree(void *ptr, size_t nelem, size_t size)'
  4293.      Frees the memory allocation beginning at PTR so the memory can be
  4294.      reused by another call to allocate memory.  If PTR is `NULL' then
  4295.      no memory will be freed.  All of the previous contents will be
  4296.      destroyed.  The NELEM and SIZE parameters are ignored in this
  4297.      implementation.  This function is available for backwards
  4298.      compatibility with older C libraries and `calloc()' and should not
  4299.      be used in new code.
  4300.  
  4301.    The following 5 functions are available as replacements for existing
  4302. C++ library functions, but the replacements in `mpatrol.h' will only be
  4303. used if the `MP_NOCPLUSPLUS' preprocessor macro is not defined.  To use
  4304. these you must include `mpatrol.h' before all other header files,
  4305. although on UNIX and Windows platforms (and AmigaOS when using `gcc')
  4306. they will be used anyway, albeit with slightly less tracing information.
  4307.  
  4308. `void *operator new(size_t size)'
  4309.      Allocates SIZE uninitialised bytes from the heap and returns a
  4310.      pointer to the first byte of the allocation.  The pointer returned
  4311.      will be suitably aligned for casting to any type and can be used
  4312.      to store data of up to SIZE bytes in length.  If SIZE is `0' then
  4313.      the memory allocated will be implicitly rounded up to `1' byte.
  4314.      If there is not enough space in the heap then the `NULL' pointer
  4315.      will be returned and `errno' will be set to `ENOMEM' -- no
  4316.      exceptions will be thrown.  The allocated memory must be
  4317.      deallocated with `operator delete'.
  4318.  
  4319. `void *operator new[](size_t size)'
  4320.      Allocates SIZE uninitialised bytes from the heap and returns a
  4321.      pointer to the first byte of the allocation.  The pointer returned
  4322.      will be suitably aligned for casting to any type and can be used
  4323.      to store data of up to SIZE bytes in length.  If SIZE is `0' then
  4324.      the memory allocated will be implicitly rounded up to `1' byte.
  4325.      If there is not enough space in the heap then the `NULL' pointer
  4326.      will be returned and `errno' will be set to `ENOMEM' -- no
  4327.      exceptions will be thrown.  The allocated memory must be
  4328.      deallocated with `operator delete[]'.
  4329.  
  4330. `void operator delete(void *ptr)'
  4331.      Frees the memory allocation beginning at PTR so the memory can be
  4332.      reused by another call to allocate memory.  If PTR is `NULL' then
  4333.      no memory will be freed.  All of the previous contents will be
  4334.      destroyed.  This function must only be used with memory allocated
  4335.      by `operator new'.
  4336.  
  4337. `void operator delete[](void *ptr)'
  4338.      Frees the memory allocation beginning at PTR so the memory can be
  4339.      reused by another call to allocate memory.  If PTR is `NULL' then
  4340.      no memory will be freed.  All of the previous contents will be
  4341.      destroyed.  This function must only be used with memory allocated
  4342.      by `operator new[]'.
  4343.  
  4344. `void (*set_new_handler(void (*func)(void)))(void)'
  4345.      Installs a low-memory handler specifically for use with `operator
  4346.      new' and `operator new[]' and returns a pointer to the previously
  4347.      installed handler, or the `NULL' pointer if no handler had been
  4348.      previously installed.  This will be called repeatedly by both
  4349.      functions when they would normally return `NULL', and this loop
  4350.      will continue until they manage to allocate the requested space.
  4351.      The default low-memory handler for the C++ operators will
  4352.      terminate the program and write an out of memory message to the
  4353.      log file.  Note that this function is equivalent to
  4354.      `__mp_nomemory()' and will replace the handler installed by that
  4355.      function.
  4356.  
  4357.    The following 10 functions are available as replacements for
  4358. existing C library memory operation functions.  To use these you must
  4359. include `mpatrol.h' before all other header files, although on UNIX and
  4360. Windows platforms (and AmigaOS when using `gcc') they will be used
  4361. anyway, albeit with slightly less tracing information.
  4362.  
  4363. `void *memset(void *ptr, int byte, size_t size)'
  4364.      Writes SIZE bytes of value BYTE to the memory location beginning at
  4365.      PTR and returns PTR.  If SIZE is `0' then no bytes will be
  4366.      written.  If the operation would affect an existing memory
  4367.      allocation in the heap but would straddle that allocation's
  4368.      boundaries then an error message will be generated in the log file
  4369.      and no bytes will be written.
  4370.  
  4371. `void bzero(void *ptr, size_t size)'
  4372.      Writes SIZE zero bytes to the memory location beginning at PTR.  If
  4373.      SIZE is `0' then no bytes will be written.  If the operation would
  4374.      affect an existing memory allocation in the heap but would
  4375.      straddle that allocation's boundaries then an error message will
  4376.      be generated in the log file and no bytes will be written.  This
  4377.      function is available for backwards compatibility with older C
  4378.      libraries and should not be used in new code.
  4379.  
  4380. `void *memccpy(void *dest, const void *src, int byte, size_t size)'
  4381.      Copies SIZE bytes from SRC to DEST and returns `NULL', or copies
  4382.      the number of bytes up to and including the first occurrence of
  4383.      BYTE if BYTE exists within the specified range and returns a
  4384.      pointer to the first byte after BYTE.  If SIZE is `0' or SRC is
  4385.      the same as DEST then no bytes will be copied.  The source and
  4386.      destination ranges should not overlap, otherwise a warning will be
  4387.      written to the log file.  If the operation would affect an
  4388.      existing memory allocation in the heap but would straddle that
  4389.      allocation's boundaries then an error message will be generated in
  4390.      the log file and no bytes will be copied.
  4391.  
  4392. `void *memcpy(void *dest, const void *src, size_t size)'
  4393.      Copies SIZE bytes from SRC to DEST and returns DEST.  If SIZE is
  4394.      `0' or SRC is the same as DEST then no bytes will be copied.  The
  4395.      source and destination ranges should not overlap, otherwise a
  4396.      warning will be written to the log file.  If the operation would
  4397.      affect an existing memory allocation in the heap but would
  4398.      straddle that allocation's boundaries then an error message will
  4399.      be generated in the log file and no bytes will be copied.
  4400.  
  4401. `void *memmove(void *dest, const void *src, size_t size)'
  4402.      Copies SIZE bytes from SRC to DEST and returns DEST.  If SIZE is
  4403.      `0' or SRC is the same as DEST then no bytes will be copied.  If
  4404.      the operation would affect an existing memory allocation in the
  4405.      heap but would straddle that allocation's boundaries then an error
  4406.      message will be generated in the log file and no bytes will be
  4407.      copied.
  4408.  
  4409. `void bcopy(const void *src, void *dest, size_t size)'
  4410.      Copies SIZE bytes from SRC to DEST.  If SIZE is `0' or SRC is the
  4411.      same as DEST then no bytes will be copied.  If the operation would
  4412.      affect an existing memory allocation in the heap but would
  4413.      straddle that allocation's boundaries then an error message will
  4414.      be generated in the log file and no bytes will be copied.  This
  4415.      function is available for backwards compatibility with older C
  4416.      libraries and should not be used in new code.
  4417.  
  4418. `int memcmp(const void *ptr1, const void *ptr2, size_t size)'
  4419.      Compares SIZE bytes from PTR1 and PTR2 and returns `0' if all of
  4420.      the bytes are identical, or returns the byte difference of the
  4421.      first differing bytes.  If SIZE is `0' or PTR1 is the same as PTR2
  4422.      then no bytes will be compared.  If the operation would read from
  4423.      an existing memory allocation in the heap but would straddle that
  4424.      allocation's boundaries then an error message will be generated in
  4425.      the log file and no bytes will be compared.
  4426.  
  4427. `int bcmp(const void *ptr1, const void *ptr2, size_t size)'
  4428.      Compares SIZE bytes from PTR1 and PTR2 and returns `0' if all of
  4429.      the bytes are identical, or returns the byte difference of the
  4430.      first differing bytes.  If SIZE is `0' or PTR1 is the same as PTR2
  4431.      then no bytes will be compared.  If the operation would read from
  4432.      an existing memory allocation in the heap but would straddle that
  4433.      allocation's boundaries then an error message will be generated in
  4434.      the log file and no bytes will be compared.  This function is
  4435.      available for backwards compatibility with older C libraries and
  4436.      should not be used in new code.
  4437.  
  4438. `void *memchr(const void *ptr, int byte, size_t size)'
  4439.      Searches up to SIZE bytes in PTR for the first occurrence of BYTE
  4440.      and returns a pointer to it or `NULL' if no such byte occurs.  If
  4441.      SIZE is `0' then no bytes will be searched.  If the operation
  4442.      would affect an existing memory allocation in the heap but would
  4443.      straddle that allocation's boundaries then an error message will
  4444.      be generated in the log file and no bytes will be searched.
  4445.  
  4446. `void *memmem(const void *ptr1, size_t size1, const void *ptr2, size_t size2)'
  4447.      Searches up to SIZE1 bytes in PTR1 for the first occurrence of
  4448.      PTR2 (which is exactly SIZE2 bytes in length) and returns a pointer
  4449.      to it or `NULL' if no such sequence of bytes occur.  If SIZE1 or
  4450.      SIZE2 is `0' then no bytes will be searched.  If the operation
  4451.      would affect an existing memory allocation in the heap but would
  4452.      straddle that allocation's boundaries then an error message will
  4453.      be generated in the log file and no bytes will be searched.
  4454.  
  4455.    The following 8 functions are available as support routines for
  4456. additional control and tracing in the mpatrol library.  To use these
  4457. you should include the `mpatrol.h' header file.
  4458.  
  4459. `int __mp_info(const void *ptr, __mp_allocinfo *info)'
  4460.      Obtains information about a specific memory allocation by placing
  4461.      statistics about PTR in INFO.  If PTR does not belong to a
  4462.      previously allocated memory allocation then `0' will be returned,
  4463.      otherwise `1' will be returned and INFO will contain the following
  4464.      information:
  4465.  
  4466.      _Field_       _Description_
  4467.      `block'       Pointer to first byte of allocation.
  4468.      `size'        Size of allocation in bytes.
  4469.      `type'        Type of function which allocated memory.
  4470.      `alloc'       Allocation index.
  4471.      `realloc'     Number of times reallocated.
  4472.      `thread'      Thread identifier.
  4473.      `func'        Function in which allocation took place.
  4474.      `file'        File in which allocation took place.
  4475.      `line'        Line number at which allocation took place.
  4476.      `stack'       Pointer to function call stack.
  4477.      `freed'       Indicates if allocation has been freed.
  4478.  
  4479. `int __mp_printinfo(const void *ptr)'
  4480.      Displays information about a specific memory allocation containing
  4481.      PTR to the standard error file stream.  If PTR does not belong to
  4482.      a previously allocated memory allocation then `0' will be
  4483.      returned, otherwise `1' will be returned.  This function is
  4484.      intended to be called from within a debugger.
  4485.  
  4486. `void __mp_memorymap(int stats)'
  4487.      If STATS is non-zero then the current statistics of the mpatrol
  4488.      library will be displayed.  If the heap contains at least one
  4489.      allocated, freed or free block then a map of the current heap will
  4490.      also be displayed.
  4491.  
  4492. `void __mp_summary(void)'
  4493.      Displays information about the current state of the mpatrol
  4494.      library, including its settings and any relevant statistics.
  4495.  
  4496. `void __mp_check(void)'
  4497.      Forces the library to perform an immediate check of the overflow
  4498.      buffers of every memory allocation and to ensure that nothing has
  4499.      overwritten any free blocks.
  4500.  
  4501. `void (*__mp_prologue(void (*func)(const void *, size_t)))(const void *, size_t)'
  4502.      Installs a prologue function to be called before any memory
  4503.      allocation, reallocation or deallocation function.  This function
  4504.      will return a pointer to the previously installed prologue
  4505.      function, or the `NULL' pointer if no prologue function had been
  4506.      previously installed.  The following arguments will be used to
  4507.      call the prologue function:
  4508.  
  4509.      _Argument 1_  _Argument 2_  _Called by_
  4510.      `-1'          SIZE          `malloc()', etc.
  4511.      PTR           SIZE          `realloc()', etc.
  4512.      PTR           `-1'          `free()', etc.
  4513.      PTR           `-2'          `strdup()', etc.
  4514.  
  4515. `void (*__mp_epilogue(void (*func)(const void *)))(const void *)'
  4516.      Installs an epilogue function to be called after any memory
  4517.      allocation, reallocation or deallocation function.  This function
  4518.      will return a pointer to the previously installed epilogue
  4519.      function, or the `NULL' pointer if no epilogue function had been
  4520.      previously installed.  The following arguments will be used to
  4521.      call the epilogue function:
  4522.  
  4523.      _Argument_    _Called by_
  4524.      PTR           `malloc()', `realloc()', `strdup()', etc.
  4525.      `-1'          `free()', etc.
  4526.  
  4527. `void (*__mp_nomemory(void (*func)(void)))(void)'
  4528.      Installs a low-memory handler and returns a pointer to the
  4529.      previously installed handler, or the `NULL' pointer if no handler
  4530.      had been previously installed.  This will be called once by C
  4531.      memory allocation functions, and repeatedly by C++ memory
  4532.      allocation functions, when they would normally return `NULL'.  Note
  4533.      that this function is equivalent to `set_new_handler()' and will
  4534.      replace the handler installed by that function.
  4535.  
  4536. Appendix B Environment
  4537. **********************
  4538.  
  4539.    The library can read certain options at run-time from an environment
  4540. variable called `MPATROL_OPTIONS'.  This variable must contain one or
  4541. more valid option keywords from the list below and must be no longer
  4542. than 1024 characters in length.  If `MPATROL_OPTIONS' is unset or empty
  4543. then the default settings will be used.
  4544.  
  4545.    The syntax for options specified within the `MPATROL_OPTIONS'
  4546. environment variable is `OPTION' or `OPTION=VALUE', where `OPTION' is a
  4547. keyword from the list below and `VALUE' is the setting for that option.
  4548. If `VALUE' is numeric then it may be specified using binary, octal,
  4549. decimal or hexadecimal notation, with binary notation beginning with
  4550. either `0b' or `0B'.  If `VALUE' is a character string containing
  4551. spaces then it may be quoted using double quotes.  No whitespace may
  4552. appear between the `=' sign, but whitespace must appear between
  4553. different options.  Note that option keywords can be given in lowercase
  4554. as well as uppercase, or a mixture of both.
  4555.  
  4556. `ALLOCBYTE'=<UNSIGNED-INTEGER>
  4557.      Specifies an 8-bit byte pattern with which to prefill
  4558.      newly-allocated memory.  This can be used to detect the use of
  4559.      memory which has not been initialised after allocation.  Note that
  4560.      this setting will not affect memory allocated with `calloc()' or
  4561.      `recalloc()' as these functions always prefill allocated memory
  4562.      with an 8-bit byte pattern of zero.  Default value:
  4563.      `ALLOCBYTE=0xFF'.
  4564.  
  4565. `ALLOCSTOP'=<UNSIGNED-INTEGER>
  4566.      Specifies an allocation index at which to stop the program when it
  4567.      is being allocated.  When the number of memory allocations reaches
  4568.      this number the program will be halted, and its state may be
  4569.      examined at that point by using a suitable debugger.  Note that
  4570.      this setting will be ignored if its value is zero.  Default value:
  4571.      `ALLOCSTOP=0'.
  4572.  
  4573. `ALLOWOFLOW'
  4574.      Specifies that a warning rather than an error should be produced
  4575.      if any memory operation function overflows the boundaries of a
  4576.      memory allocation, and that the operation should still be
  4577.      performed.  This option is provided for circumstances where it is
  4578.      desirable for the memory operation to be performed, regardless of
  4579.      whether it is erroneous or not.
  4580.  
  4581. `AUTOSAVE'=<UNSIGNED-INTEGER>
  4582.      Specifies the frequency at which to periodically write the
  4583.      profiling data to the profiling output file.  When the total
  4584.      number of profiled memory allocations and deallocations is a
  4585.      multiple of this number then the current profiling information
  4586.      will be written to the profiling output file.  This option can be
  4587.      used to instruct the mpatrol library to dump out any profiling
  4588.      information just before a fatal error occurs in a program, for
  4589.      example.  Note that this setting will be ignored if its value is
  4590.      zero.  Default value: `AUTOSAVE=0'.
  4591.  
  4592. `CHECK'=<UNSIGNED-RANGE>
  4593.      Specifies a range of allocation indices at which to check the
  4594.      integrity of free memory and overflow buffers.  The range must be
  4595.      specified as no more than two unsigned integers separated by a
  4596.      dash.  If numbers on either the left side or the right side of the
  4597.      dash are omitted then they will be assumed to be `0' and INFINITY
  4598.      respectively.  A value of `0' on its own indicates that no such
  4599.      checking will ever be performed.  This option can be used to speed
  4600.      up the execution speed of the library at the expense of checking.
  4601.      Default value: `CHECK=-'.
  4602.  
  4603. `CHECKALL'
  4604.      Equivalent to the `CHECKALLOCS', `CHECKREALLOCS' and `CHECKFREES'
  4605.      options specified together.
  4606.  
  4607. `CHECKALLOCS'
  4608.      Checks that no attempt is made to allocate a block of memory of
  4609.      size zero.  A warning will be issued for every such case.
  4610.  
  4611. `CHECKFREES'
  4612.      Checks that no attempt is made to deallocate a `NULL' pointer.  A
  4613.      warning will be issued for every such case.
  4614.  
  4615. `CHECKREALLOCS'
  4616.      Checks that no attempt is made to reallocate a `NULL' pointer or
  4617.      resize an existing block of memory to size zero.  Warnings will be
  4618.      issued for every such case.
  4619.  
  4620. `DEFALIGN'=<UNSIGNED-INTEGER>
  4621.      Specifies the default alignment for general-purpose memory
  4622.      allocations, which must be a power of two (and will be rounded up
  4623.      to the nearest power of two if it is not).  The default alignment
  4624.      for a particular system is calculated at run-time.
  4625.  
  4626. `FAILFREQ'=<UNSIGNED-INTEGER>
  4627.      Specifies the frequency at which all memory allocations will
  4628.      randomly fail.  For example, a value of `10' will mean that
  4629.      roughly 1 in 10 memory allocations will fail, but a value of `0'
  4630.      will disable all random failures.  This option can be useful for
  4631.      stress-testing an application.  Default value: `FAILFREQ=0'.
  4632.  
  4633. `FAILSEED'=<UNSIGNED-INTEGER>
  4634.      Specifies the random number seed which will be used when
  4635.      determining which memory allocations will randomly fail.  A value
  4636.      of `0' will instruct the library to pick a random seed every time
  4637.      it is run.  Any other value will mean that the random failures
  4638.      will be the same every time the program is run, but only as long
  4639.      as the seed stays the same.  Default value: `FAILSEED=0'.
  4640.  
  4641. `FREEBYTE'=<UNSIGNED-INTEGER>
  4642.      Specifies an 8-bit byte pattern with which to prefill newly-freed
  4643.      memory.  This can be used to detect the use of memory which has
  4644.      just been freed.  It is also used internally to ensure that freed
  4645.      memory has not been overwritten.  Note that the freed memory may
  4646.      be reused the next time a block of memory is allocated and so once
  4647.      memory has been freed its contents are not guaranteed to remain
  4648.      the same as the specified byte pattern.  Default value:
  4649.      `FREEBYTE=0x55'.
  4650.  
  4651. `FREESTOP'=<UNSIGNED-INTEGER>
  4652.      Specifies an allocation index at which to stop the program when it
  4653.      is being freed.  When the memory allocation with the specified
  4654.      allocation index is to be freed the program will be halted, and
  4655.      its state may be examined at that point using a suitable debugger.
  4656.      Note that this setting will be ignored if its value is zero.
  4657.      Default value: `FREESTOP=0'.
  4658.  
  4659. `HELP'
  4660.      Displays a quick-reference option summary to the `stderr' file
  4661.      stream.
  4662.  
  4663. `LARGEBOUND'=<UNSIGNED-INTEGER>
  4664.      Specifies the limit in bytes up to which memory allocations should
  4665.      be classified as large allocations for profiling purposes.  This
  4666.      limit must be greater than the small and medium bounds.  Default
  4667.      value: `LARGEBOUND=2048'.
  4668.  
  4669. `LIMIT'=<UNSIGNED-INTEGER>
  4670.      Specifies the limit in bytes at which all memory allocations
  4671.      should fail if the total allocated memory should increase beyond
  4672.      this.  This can be used to stress-test software to see how it
  4673.      behaves in low memory conditions.  The internal memory used by the
  4674.      library itself will not be counted as part of the total heap size,
  4675.      but on some systems there may be a small amount of memory required
  4676.      to initialise the library itself.  Note that this setting will be
  4677.      ignored if its value is zero.  Default value: `LIMIT=0'.
  4678.  
  4679. `LOGALL'
  4680.      Equivalent to the `LOGALLOCS', `LOGREALLOCS', `LOGFREES' and
  4681.      `LOGMEMORY' options specified together.
  4682.  
  4683. `LOGALLOCS'
  4684.      Specifies that all memory allocations are to be logged and sent to
  4685.      the log file.  Note that any memory allocations made internally by
  4686.      the library will not be logged.
  4687.  
  4688. `LOGFILE'=<STRING>
  4689.      Specifies an alternative file in which to place all diagnostics
  4690.      from the mpatrol library.  A filename of `stderr' will send all
  4691.      diagnostics to the `stderr' file stream and a filename of `stdout'
  4692.      will do the equivalent with the `stdout' file stream.  Note that
  4693.      if a problem occurs while opening the log file or if any
  4694.      diagnostics require to be displayed before the log file has had a
  4695.      chance to be opened then they will be sent to the `stderr' file
  4696.      stream.  Default value: `LOGFILE=mpatrol.log'.
  4697.  
  4698. `LOGFREES'
  4699.      Specifies that all memory deallocations are to be logged and sent
  4700.      to the log file.  Note that any memory deallocations made
  4701.      internally by the library will not be logged.
  4702.  
  4703. `LOGMEMORY'
  4704.      Specifies that all memory operations are to be logged and sent to
  4705.      the log file.  These operations will be made by calls to functions
  4706.      such as `memset()' and `memcpy()'.  Note that any memory
  4707.      operations made internally by the library will not be logged.
  4708.  
  4709. `LOGREALLOCS'
  4710.      Specifies that all memory reallocations are to be logged and sent
  4711.      to the log file.  Note that any memory reallocations made
  4712.      internally by the library will not be logged.
  4713.  
  4714. `MEDIUMBOUND'=<UNSIGNED-INTEGER>
  4715.      Specifies the limit in bytes up to which memory allocations should
  4716.      be classified as medium allocations for profiling purposes.  This
  4717.      limit must be greater than the small bound but less than the large
  4718.      bound.  Default value: `MEDIUMBOUND=256'.
  4719.  
  4720. `NOFREE'
  4721.      Specifies that the mpatrol library should keep all reallocated and
  4722.      freed memory allocations.  Such freed memory allocations will then
  4723.      be flagged as freed and can be used by the library to provide
  4724.      better diagnostics.  However, as no system memory will ever be
  4725.      reused by the mpatrol library, this option can quickly lead to a
  4726.      shortage of available system memory for a process.  Note that this
  4727.      option will always force a memory reallocation to return a pointer
  4728.      to newly-allocated memory, but the `expand()' function will never
  4729.      be affected by this option.
  4730.  
  4731. `NOPROTECT'
  4732.      Specifies that the mpatrol library's internal data structures
  4733.      should not be made read-only after every memory allocation
  4734.      reallocation or deallocation.  This may significantly speed up
  4735.      execution but this will be at the expense of less safety if the
  4736.      program accidentally overwrites some of the library's internal data
  4737.      structures.  Note that this option has no effect on systems that
  4738.      do not support memory protection.
  4739.  
  4740. `OFLOWBYTE'=<UNSIGNED-INTEGER>
  4741.      Specifies an 8-bit byte pattern with which to fill the overflow
  4742.      buffers of all memory allocations.  This is used internally to
  4743.      ensure that nothing has been written beyond the beginning or the
  4744.      end of a block of allocated memory.  Note that this setting will
  4745.      only have an effect if the `OFLOWSIZE' option is in use.  Default
  4746.      value: `OFLOWBYTE=0xAA'.
  4747.  
  4748. `OFLOWSIZE'=<UNSIGNED-INTEGER>
  4749.      Specifies the size in bytes to use for all overflow buffers, which
  4750.      must be a power of two (and will be rounded up to the nearest
  4751.      power of two if it is not).  This is used internally to ensure
  4752.      that nothing has been written beyond the beginning or the end of a
  4753.      block of allocated memory.  Note that this setting specifies the
  4754.      size for only one of the overflow buffers given to each memory
  4755.      allocation; the other overflow buffer will have an identical size.
  4756.      No overflow buffers will be used if this setting is zero.
  4757.      Default value: `OFLOWSIZE=0'.
  4758.  
  4759. `OFLOWWATCH'
  4760.      Specifies that watch point areas should be used for overflow
  4761.      buffers rather than filling with the overflow byte.  This can
  4762.      significantly reduce the speed of program execution.  Note that
  4763.      this option has no effect on systems that do not support watch
  4764.      point areas.
  4765.  
  4766. `PAGEALLOC'=<`LOWER'|`UPPER'>
  4767.      Specifies that each individual memory allocation should occupy at
  4768.      least one page of virtual memory and should be placed at the
  4769.      lowest or highest point within these pages.  This allows the
  4770.      library to place an overflow buffer of one page on either side of
  4771.      every memory allocation and write-protect these pages as well as
  4772.      all free and freed memory.  Note that this option has no effect on
  4773.      systems that do not support memory protection, and is disabled by
  4774.      default on other systems as it can slow down the speed of program
  4775.      execution.
  4776.  
  4777. `PRESERVE'
  4778.      Specifies that any reallocated or freed memory allocations should
  4779.      preserve their original contents.  This option must be used with
  4780.      the `NOFREE' option and has no effect otherwise.
  4781.  
  4782. `PROF'
  4783.      Specifies that all memory allocations and deallocations are to be
  4784.      profiled and sent to the profiling output file.  Memory
  4785.      reallocations are treated as a memory deallocation immediately
  4786.      followed by a memory allocation.
  4787.  
  4788. `PROFFILE'=<STRING>
  4789.      Specifies an alternative file in which to place all memory
  4790.      allocation profiling information from the mpatrol library.  A
  4791.      filename of `stderr' will send this information to the `stderr'
  4792.      file stream and a filename of `stdout' will do the equivalent with
  4793.      the `stdout' file stream.  Note that if a problem occurs while
  4794.      opening the profiling output file then the profiling information
  4795.      will be sent to the `stderr' file stream.  Default value:
  4796.      `PROFFILE=mpatrol.out'.
  4797.  
  4798. `PROGFILE'=<STRING>
  4799.      Specifies an alternative filename with which to locate the
  4800.      executable file containing the program's symbols.  On most
  4801.      systems, the library will automatically be able to determine this
  4802.      filename, but on a few systems this option may have to be used
  4803.      before any or all symbols can be read.
  4804.  
  4805. `REALLOCSTOP'=<UNSIGNED-INTEGER>
  4806.      Specifies a reallocation index at which to stop the program when a
  4807.      memory allocation is being reallocated.  If the `ALLOCSTOP' option
  4808.      is non-zero then the program will be halted when the allocation
  4809.      matching that allocation index is reallocated the specified number
  4810.      of times.  Otherwise the program will be halted the first time any
  4811.      allocation is reallocated the specified number of times.  Note
  4812.      that this setting will be ignored if its value is zero.  Default
  4813.      value: `REALLOCSTOP=0'.
  4814.  
  4815. `SAFESIGNALS'
  4816.      Instructs the library to save and replace certain signal handlers
  4817.      during the execution of library code and to restore them
  4818.      afterwards.  This was the default behaviour in version 1.0 of the
  4819.      mpatrol library and was changed since some memory-intensive
  4820.      programs became very hard to interrupt using the keyboard, thus
  4821.      giving the impression that the program or system had hung.
  4822.  
  4823. `SHOWALL'
  4824.      Equivalent to the `SHOWFREED', `SHOWUNFREED', `SHOWMAP' and
  4825.      `SHOWSYMBOLS' options specified together.
  4826.  
  4827. `SHOWFREED'
  4828.      Specifies that a summary of all of the freed memory allocations
  4829.      should be displayed at the end of program execution.  This option
  4830.      must be used in conjunction with the `NOFREE' option and this step
  4831.      will not be performed if an abnormal termination occurs or if
  4832.      there were no freed allocations.
  4833.  
  4834. `SHOWMAP'
  4835.      Specifies that a memory map of the entire heap should be displayed
  4836.      at the end of program execution.  This step will not be performed
  4837.      if an abnormal termination occurs or if the heap is empty.
  4838.  
  4839. `SHOWSYMBOLS'
  4840.      Specifies that a summary of all of the function symbols read from
  4841.      the program's executable file should be displayed at the end of
  4842.      program execution.  This step will not be performed if an abnormal
  4843.      termination occurs or if no symbols could be read from the
  4844.      executable file.
  4845.  
  4846. `SHOWUNFREED'
  4847.      Specifies that a summary of all of the unfreed memory allocations
  4848.      should be displayed at the end of program execution.  This step
  4849.      will not be performed if an abnormal termination occurs or if
  4850.      there are no unfreed allocations.
  4851.  
  4852. `SMALLBOUND'=<UNSIGNED-INTEGER>
  4853.      Specifies the limit in bytes up to which memory allocations should
  4854.      be classified as small allocations for profiling purposes.  This
  4855.      limit must be greater than zero but less than the medium and large
  4856.      bounds.  Default value: `SMALLBOUND=32'.
  4857.  
  4858. `UNFREEDABORT'=<UNSIGNED-INTEGER>
  4859.      Specifies the minimum number of unfreed allocations at which to
  4860.      abort the program just before program termination.  A summary of
  4861.      all the allocations will be displayed on the standard error file
  4862.      stream before aborting.  This option may be handy for use in batch
  4863.      tests as it can force tests to fail if they do not free up a
  4864.      minimum number of memory allocations.  Note that this setting will
  4865.      be ignored if its value is zero.  Default value: `UNFREEDABORT=0'.
  4866.  
  4867. `USEDEBUG'
  4868.      Specifies that any debugging information in the executable file
  4869.      should be used to obtain additional source-level information.
  4870.      This option will only have an effect if the executable file
  4871.      contains a compiler-generated line number table and will be
  4872.      ignored if the mpatrol library was built to support an object file
  4873.      access library that cannot read line tables from object files.
  4874.      Note that this option will slow down program execution, use up
  4875.      more system memory and may leave unaccounted unfreed memory
  4876.      allocations at program termination.
  4877.  
  4878. `USEMMAP'
  4879.      Specifies that the library should use `mmap()' instead of `sbrk()'
  4880.      to allocate system memory on UNIX platforms.  This option should
  4881.      be used if there are problems when using the mpatrol library in
  4882.      combination with another malloc library which uses `sbrk()' to
  4883.      allocate its memory.  It is ignored on systems that do not support
  4884.      the `mmap()' system call.
  4885.  
  4886. Appendix C Options
  4887. ******************
  4888.  
  4889.    A utility program called `mpatrol' is provided to run commands that
  4890. have been linked with the mpatrol library.
  4891.  
  4892.      mpatrol [options] <command> [arguments]
  4893.  
  4894.    The `mpatrol' command is used to set various mpatrol library OPTIONS
  4895. when running COMMAND with its ARGUMENTS.  In most cases, COMMAND must
  4896. have been linked with the mpatrol library, unless the `-d' option is
  4897. used in which case COMMAND need only have been dynamically linked.
  4898.  
  4899.    All mpatrol library diagnostics are sent to the file
  4900. `mpatrol.%n.log' in the current directory by default (where `%n' is the
  4901. current process id) but this can be changed using the `-l' option.
  4902. Similarly, the default profiling output filename is `mpatrol.%n.out'.
  4903. Note that the `LOGALL' option is always implicitly used for commands
  4904. that are run by this command.
  4905.  
  4906.    Alternatively, the log file and profiling output file names can
  4907. contain `%p', which will be replaced with the name of the program being
  4908. executed without the directory components.  If the executable filename
  4909. could not be determined or was not set then it will be replaced with
  4910. `mpatrol'.
  4911.  
  4912.    All of the following options (except `-d' and `-V') correspond to
  4913. their listed mpatrol library option (*note Environment::).
  4914.  
  4915. `-1' <UNSIGNED-INTEGER>
  4916.      [`SMALLBOUND']  Specifies the limit in bytes up to which memory
  4917.      allocations should be classified as small allocations for
  4918.      profiling purposes.
  4919.  
  4920. `-2' <UNSIGNED-INTEGER>
  4921.      [`MEDIUMBOUND']  Specifies the limit in bytes up to which memory
  4922.      allocations should be classified as medium allocations for
  4923.      profiling purposes.
  4924.  
  4925. `-3' <UNSIGNED-INTEGER>
  4926.      [`LARGEBOUND']  Specifies the limit in bytes up to which memory
  4927.      allocations should be classified as large allocations for
  4928.      profiling purposes.
  4929.  
  4930. `-A' <UNSIGNED-INTEGER>
  4931.      [`ALLOCSTOP']  Specifies an allocation index at which to stop the
  4932.      program when it is being allocated.
  4933.  
  4934. `-a' <UNSIGNED-INTEGER>
  4935.      [`ALLOCBYTE']  Specifies an 8-bit byte pattern with which to
  4936.      prefill newly-allocated memory.
  4937.  
  4938. `-C' <UNSIGNED-RANGE>
  4939.      [`CHECK']  Specifies a range of allocation indices at which to
  4940.      check the integrity of free memory and overflow buffers.
  4941.  
  4942. `-c'
  4943.      [`CHECKALL']  Specifies that all arguments to functions which
  4944.      allocate, reallocate and deallocate memory have rigorous checks
  4945.      performed on them.
  4946.  
  4947. `-D' <UNSIGNED-INTEGER>
  4948.      [`DEFALIGN']  Specifies the default alignment for general-purpose
  4949.      memory allocations, which must be a power of two.
  4950.  
  4951. `-d'
  4952.      Specifies that programs which were not linked with the mpatrol
  4953.      library should also be traced, but only if they were dynamically
  4954.      linked.  This option will only work if the system dynamic linker
  4955.      has the ability to preload a set of user-specified shared
  4956.      libraries via a special environment variable.
  4957.  
  4958. `-e' <STRING>
  4959.      [`PROGFILE']  Specifies an alternative filename with which to
  4960.      locate the executable file containing the program's symbols.
  4961.  
  4962. `-F' <UNSIGNED-INTEGER>
  4963.      [`FREESTOP']  Specifies an allocation index at which to stop the
  4964.      program when it is being freed.
  4965.  
  4966. `-f' <UNSIGNED-INTEGER>
  4967.      [`FREEBYTE']  Specifies an 8-bit byte pattern with which to prefill
  4968.      newly-freed memory.
  4969.  
  4970. `-G'
  4971.      [`SAFESIGNALS']  Instructs the library to save and replace certain
  4972.      signal handlers during the execution of library code and to
  4973.      restore them afterwards.
  4974.  
  4975. `-g'
  4976.      [`USEDEBUG']  Specifies that any debugging information in the
  4977.      executable file should be used to obtain additional source-level
  4978.      information.
  4979.  
  4980. `-L' <UNSIGNED-INTEGER>
  4981.      [`LIMIT']  Specifies the limit in bytes at which all memory
  4982.      allocations should fail if the total allocated memory should
  4983.      increase beyond this.
  4984.  
  4985. `-l' <STRING>
  4986.      [`LOGFILE']  Specifies an alternative file in which to place all
  4987.      diagnostics from the mpatrol library.
  4988.  
  4989. `-M'
  4990.      [`ALLOWOFLOW']  Specifies that a warning rather than an error
  4991.      should be produced if any memory operation function overflows the
  4992.      boundaries of a memory allocation, and that the operation should
  4993.      still be performed.
  4994.  
  4995. `-m'
  4996.      [`USEMMAP']  Specifies that the library should use `mmap()' instead
  4997.      of `sbrk()' to allocate system memory.
  4998.  
  4999. `-N'
  5000.      [`NOPROTECT']  Specifies that the mpatrol library's internal data
  5001.      structures should not be made read-only after every memory
  5002.      allocation, reallocation or deallocation.
  5003.  
  5004. `-n'
  5005.      [`NOFREE']  Specifies that the mpatrol library should keep all
  5006.      reallocated and freed memory allocations.
  5007.  
  5008. `-O' <UNSIGNED-INTEGER>
  5009.      [`OFLOWSIZE']  Specifies the size in bytes to use for all overflow
  5010.      buffers, which must be a power of two.
  5011.  
  5012. `-o' <UNSIGNED-INTEGER>
  5013.      [`OFLOWBYTE']  Specifies an 8-bit byte pattern with which to fill
  5014.      the overflow buffers of all memory allocations.
  5015.  
  5016. `-P' <STRING>
  5017.      [`PROFFILE']  Specifies an alternative file in which to place all
  5018.      memory allocation profiling information from the mpatrol library.
  5019.  
  5020. `-p'
  5021.      [`PROF']  Specifies that all memory allocations are to be profiled
  5022.      and sent to the profiling output file.
  5023.  
  5024. `-Q' <UNSIGNED-INTEGER>
  5025.      [`AUTOSAVE']  Specifies the frequency at which to periodically
  5026.      write the profiling data to the profiling output file.
  5027.  
  5028. `-R' <UNSIGNED-INTEGER>
  5029.      [`REALLOCSTOP']  Specifies an allocation index at which to stop the
  5030.      program when a memory allocation is being reallocated.
  5031.  
  5032. `-S'
  5033.      [`SHOWMAP' & `SHOWSYMBOLS']  Specifies that a memory map of the
  5034.      entire heap and a summary of all of the function symbols read from
  5035.      the program's executable file should be displayed at the end of
  5036.      program execution.
  5037.  
  5038. `-s'
  5039.      [`SHOWFREED' & `SHOWUNFREED']  Specifies that a summary of all of
  5040.      the freed and unfreed memory allocations should be displayed at
  5041.      the end of program execution.
  5042.  
  5043. `-U' <UNSIGNED-INTEGER>
  5044.      [`UNFREEDABORT']  Specifies the minimum number of unfreed
  5045.      allocations at which to abort the program just before program
  5046.      termination.
  5047.  
  5048. `-V'
  5049.      Displays the version number of the `mpatrol' command.
  5050.  
  5051. `-v'
  5052.      [`PRESERVE']  Specifies that any reallocated or freed memory
  5053.      allocations should preserve their original contents.
  5054.  
  5055. `-w'
  5056.      [`OFLOWWATCH']  Specifies that watch point areas should be used for
  5057.      overflow buffers rather than filling with the overflow byte.
  5058.  
  5059. `-X'
  5060.      [`PAGEALLOC=UPPER']  Specifies that each individual memory
  5061.      allocation should occupy at least one page of virtual memory and
  5062.      should be placed at the highest point within these pages.
  5063.  
  5064. `-x'
  5065.      [`PAGEALLOC=LOWER']  Specifies that each individual memory
  5066.      allocation should occupy at least one page of virtual memory and
  5067.      should be placed at the lowest point within these pages.
  5068.  
  5069. `-Z' <UNSIGNED-INTEGER>
  5070.      [`FAILSEED']  Specifies the random number seed which will be used
  5071.      when determining which memory allocations will randomly fail.
  5072.  
  5073. `-z' <UNSIGNED-INTEGER>
  5074.      [`FAILFREQ']  Specifies the frequency at which all memory
  5075.      allocations will randomly fail.
  5076.  
  5077. Appendix D Library performance
  5078. ******************************
  5079.  
  5080.    The following times were obtained on a Sun Ultra 5 with an
  5081. UltraSPARC IIi processor running at 333MHz and running Solaris 7.  The
  5082. test performed was the one in `tests/pass/test1.c' and all tests were
  5083. run on a lightly loaded system, but were run several times to obtain an
  5084. average result.  Obviously, these times can only be an approximation,
  5085. but should serve to illustrate the effects on performance that each
  5086. option can have.  All times are given in seconds, and the second time
  5087. on each line was obtained with the same options plus the `NOPROTECT'
  5088. option.  Running with the `CHECK=0' option would speed things up
  5089. dramatically, albeit at the expense of less error checking.
  5090.  
  5091.    Running with basic options:
  5092.  
  5093. _no options_                                0.618       0.258
  5094. `OFLOWSIZE=2'                               0.645       0.296
  5095. `OFLOWSIZE=8'                               0.686       0.327
  5096. `PAGEALLOC=LOWER'                           7.785       7.372
  5097. `PAGEALLOC=UPPER'                           7.821       7.469
  5098.  
  5099.    Running when all freed memory allocations are kept:
  5100.  
  5101. `NOFREE'                                    0.943       0.506
  5102. `NOFREE OFLOWSIZE=2'                        1.026       0.579
  5103. `NOFREE OFLOWSIZE=8'                        1.091       0.645
  5104. `NOFREE PAGEALLOC=LOWER'                    8.013       7.598
  5105. `NOFREE PAGEALLOC=UPPER'                    8.026       7.616
  5106.  
  5107.    Running when all freed memory allocations are kept and their
  5108. contents are preserved:
  5109.  
  5110. `NOFREE PRESERVE'                           0.719       0.292
  5111. `NOFREE PRESERVE OFLOWSIZE=2'               0.792       0.367
  5112. `NOFREE PRESERVE OFLOWSIZE=8'               0.850       0.419
  5113. `NOFREE PRESERVE PAGEALLOC=LOWER'           8.043       7.616
  5114. `NOFREE PRESERVE PAGEALLOC=UPPER'           8.052       7.631
  5115.  
  5116.    Running using watch points to check the overflow buffers:
  5117.  
  5118. `OFLOWSIZE=2 OFLOWWATCH'                    Interrupted after half an
  5119.                                             hour as it still hadn't
  5120.                                             finished.
  5121.  
  5122.    Running using the Solaris 7 malloc libraries:
  5123.  
  5124. Solaris 7 malloc(3c) library                0.033
  5125. Solaris 7 malloc(3x) library                0.036
  5126. Solaris 7 bsdmalloc(3x) library             0.028
  5127. Solaris 7 mapmalloc(3x) library             0.033
  5128. Solaris 7 watchmalloc(3x) library           40.845
  5129.  
  5130. Appendix E Profiling file format
  5131. ********************************
  5132.  
  5133.    The format of the profiling output files that are produced by the
  5134. mpatrol library is described here.  Every profiling output file
  5135. contains the following components.
  5136.  
  5137.    * 4 bytes containing the characters `M', `P', `T' and `L'.
  5138.  
  5139.    * 1 unsigned integer representing the value `1'.  This is used by
  5140.      `mprof' to determine the endianness of the processor that produced
  5141.      the profiling output file so that it can decide whether to perform
  5142.      byte-swapping on the input data.
  5143.  
  5144.    * 3 unsigned integers containing the small, medium and large
  5145.      allocation bounds.
  5146.  
  5147.    * 1 unsigned integer containing the allocation bin size.  If the
  5148.      allocation bin size is greater than zero then it is followed by
  5149.      the allocation bins, the large allocation totals, the deallocation
  5150.      bins and the large deallocation totals, where the bins are arrays
  5151.      of unsigned integers with dimensions of the allocation bin size
  5152.      and the totals are unsigned integers.
  5153.  
  5154.    * 1 unsigned integer containing the number of profiling data
  5155.      structures.  If the number of profiling data structures is greater
  5156.      than zero then it is followed by the profiling data structures
  5157.      themselves, which are of the following structure.
  5158.  
  5159.         * 1 unsigned integer representing the index of this profiling
  5160.           data.
  5161.  
  5162.         * 4 unsigned integers representing the small, medium, large and
  5163.           extra large allocation counts for this profiling data.
  5164.  
  5165.         * 4 unsigned integers representing the small, medium, large and
  5166.           extra large allocation totals for this profiling data.
  5167.  
  5168.         * 4 unsigned integers representing the small, medium, large and
  5169.           extra large deallocation counts for this profiling data.
  5170.  
  5171.         * 4 unsigned integers representing the small, medium, large and
  5172.           extra large deallocation totals for this profiling data.
  5173.  
  5174.    * 1 unsigned integer containing the number of call sites.  If the
  5175.      number of call sites is greater than zero then it is followed by
  5176.      the call sites themselves, which are of the following structure.
  5177.  
  5178.         * 1 unsigned integer representing the index of this call site.
  5179.  
  5180.         * 1 unsigned integer representing the index of the parent call
  5181.           site.
  5182.  
  5183.         * 1 generic pointer representing the code address of this call
  5184.           site.
  5185.  
  5186.         * 1 unsigned integer representing the index of an associated
  5187.           symbol.
  5188.  
  5189.         * 1 unsigned integer representing the offset of the symbol name.
  5190.  
  5191.         * 1 unsigned integer representing the index of any associated
  5192.           profiling data.
  5193.  
  5194.    * 1 unsigned integer containing the number of symbol addresses.  If
  5195.      the number of symbol addresses is greater than zero then it is
  5196.      followed by the symbol addresses themselves, which are generic
  5197.      pointers.
  5198.  
  5199.    * 1 unsigned integer containing the size of the symbol name string
  5200.      table.  This is followed by the symbol name string table, which is
  5201.      an array of characters containing the nul-terminated symbol names.
  5202.  
  5203.    * 4 bytes containing the characters `M', `P', `T' and `L'.
  5204.  
  5205. Appendix F Supported systems
  5206. ****************************
  5207.  
  5208.    Following is a list of systems on which the mpatrol library has been
  5209. built and tested.  The system details include the operating system and
  5210. version, the processor type, the object file format and the C compiler
  5211. used to compile the library and tests.  The details following each
  5212. system list any features of the library that are not (or cannot be)
  5213. supported on that system.
  5214.  
  5215.    * AIX 4.1, IBM RS/6000, BFD, `cc'
  5216.         * The thread-safe version of the library does not work.
  5217.  
  5218.         * The `OFLOWWATCH' option has no effect.
  5219.  
  5220.         * Cannot automatically determine the program filename.
  5221.  
  5222.         * No support for call stack traversal.
  5223.  
  5224.         * The address of an illegal memory access cannot be determined.
  5225.  
  5226.         * The `-d' option to the `mpatrol' command has no effect.
  5227.  
  5228.    * DG/UX 4.20MU07, Intel Pentium Pro, ELF32, `gcc'
  5229.         * The thread-safe version of the library does not work.
  5230.  
  5231.         * The `OFLOWWATCH' option has no effect.
  5232.  
  5233.         * The `USEDEBUG' option has no effect.
  5234.  
  5235.         * The `-d' option to the `mpatrol' command does not work unless
  5236.           `libelf.so' is available.
  5237.  
  5238.    * DG/UX 4.11, Intel Pentium Pro, ELF32, `gcc'
  5239.         * The thread-safe version of the library does not work.
  5240.  
  5241.         * The `OFLOWWATCH' option has no effect.
  5242.  
  5243.         * The `USEDEBUG' option has no effect.
  5244.  
  5245.         * The `-d' option to the `mpatrol' command has no effect.
  5246.  
  5247.    * DG/UX 4.11, Motorola 88100, ELF32, `gcc'
  5248.         * The thread-safe version of the library does not work.
  5249.  
  5250.         * The `OFLOWWATCH' option has no effect.
  5251.  
  5252.         * The `USEDEBUG' option has no effect.
  5253.  
  5254.         * Cannot automatically determine the program filename.
  5255.  
  5256.         * Call stack traversal only works with unoptimised code.
  5257.  
  5258.         * The `-d' option to the `mpatrol' command has no effect.
  5259.  
  5260.    * DYNIX/ptx 4.5, Intel Pentium Pro, ELF32, `cc'
  5261.         * The thread-safe version of the library does not work.
  5262.  
  5263.         * The `OFLOWWATCH' option has no effect.
  5264.  
  5265.         * The `USEDEBUG' option has no effect.
  5266.  
  5267.         * The `-d' option to the `mpatrol' command has no effect.
  5268.  
  5269.    * HP/UX 10.20, HP PA/RISC 9000, BFD, `gcc'
  5270.         * The thread-safe version of the library does not work.
  5271.  
  5272.         * The `OFLOWWATCH' option has no effect.
  5273.  
  5274.         * The `USEMMAP' option has no effect.
  5275.  
  5276.         * Cannot automatically determine the program filename.
  5277.  
  5278.         * No support for call stack traversal.
  5279.  
  5280.         * The `-d' option to the `mpatrol' command has no effect.
  5281.  
  5282.    * IRIX 5.3, MIPS R4000, ELF32, `cc'
  5283.         * The thread-safe version of the library does not work.
  5284.  
  5285.         * The `OFLOWWATCH' option has no effect.
  5286.  
  5287.         * The `USEDEBUG' option has no effect.
  5288.  
  5289.         * No support for call stack traversal.
  5290.  
  5291.         * Symbols cannot be read from any shared libraries that a
  5292.           program uses.
  5293.  
  5294.    * RedHat Linux 6.0, Intel Pentium III, BFD, `gcc'
  5295.         * The thread-safe version of the library does not work.
  5296.  
  5297.         * The `OFLOWWATCH' option has no effect.
  5298.  
  5299.         * The address of an illegal memory access cannot be determined.
  5300.  
  5301.         * The `-d' option to the `mpatrol' command does not work unless
  5302.           `libiberty.so' is available.
  5303.  
  5304.    * RedHat Linux 5.1, Motorola 68040, BFD, `gcc'
  5305.         * The thread-safe version of the library does not work.
  5306.  
  5307.         * The `OFLOWWATCH' option has no effect.
  5308.  
  5309.         * The address of an illegal memory access cannot be determined.
  5310.  
  5311.         * The `-d' option to the `mpatrol' command does not work unless
  5312.           `libiberty.so' is available.
  5313.  
  5314.    * RedHat Linux 5.1, Motorola 68040, ELF32, `gcc'
  5315.         * The thread-safe version of the library does not work.
  5316.  
  5317.         * The `OFLOWWATCH' option has no effect.
  5318.  
  5319.         * The `USEDEBUG' option has no effect.
  5320.  
  5321.         * The address of an illegal memory access cannot be determined.
  5322.  
  5323.         * The `-d' option to the `mpatrol' command does not work unless
  5324.           `libelf.so' is available.
  5325.  
  5326.    * LynxOS 3.0.0, PowerPC, BFD, `gcc'
  5327.         * The thread-safe version of the library does not work.
  5328.  
  5329.         * The `OFLOWWATCH' option has no effect.
  5330.  
  5331.         * The `USEMMAP' option has no effect.
  5332.  
  5333.         * Cannot automatically determine the program filename.
  5334.  
  5335.         * No support for call stack traversal.
  5336.  
  5337.         * The address of an illegal memory access cannot be determined.
  5338.  
  5339.         * The `-d' option to the `mpatrol' command has no effect.
  5340.  
  5341.    * Solaris 2.6, Intel Pentium Pro, BFD, `gcc'
  5342.         * The thread-safe version of the library does not work.
  5343.  
  5344.    * Solaris 2.6, Intel Pentium Pro, ELF32, `gcc'
  5345.         * The thread-safe version of the library does not work.
  5346.  
  5347.         * The `USEDEBUG' option has no effect.
  5348.  
  5349.    * Solaris 7, SPARC V9, BFD, `gcc'
  5350.         * The thread-safe version of the library does not work.
  5351.  
  5352.    * Solaris 7, SPARC V9, ELF32, `gcc'
  5353.         * The thread-safe version of the library does not work.
  5354.  
  5355.         * The `USEDEBUG' option has no effect.
  5356.  
  5357.    * AmigaOS 3.1, Motorola 68040, BFD, `gcc'
  5358.         * No memory protection so the `PAGEALLOC' option has no effect.
  5359.  
  5360.         * The `OFLOWWATCH' option has no effect.
  5361.  
  5362.         * The `USEDEBUG' option has no effect.
  5363.  
  5364.         * The `USEMMAP' option has no effect.
  5365.  
  5366.         * Limited support for call stack traversal.
  5367.  
  5368.         * Limited support for reading symbols.
  5369.  
  5370.         * No detection of illegal memory accesses.
  5371.  
  5372.         * The `-d' option to the `mpatrol' command has no effect.
  5373.  
  5374.    * AmigaOS 3.1, Motorola 68040, n/a, SAS/C
  5375.         * No automatic override of `malloc()', etc., without inclusion
  5376.           of `mpatrol.h'.
  5377.  
  5378.         * No memory protection so the `PAGEALLOC' option has no effect.
  5379.  
  5380.         * The `OFLOWWATCH' option has no effect.
  5381.  
  5382.         * The `USEDEBUG' option has no effect.
  5383.  
  5384.         * The `USEMMAP' option has no effect.
  5385.  
  5386.         * No support for call stack traversal.
  5387.  
  5388.         * No support for reading symbols.
  5389.  
  5390.         * No detection of illegal memory accesses.
  5391.  
  5392.         * The `-d' option to the `mpatrol' command has no effect.
  5393.  
  5394.    * Microsoft Windows NT 4.0, Intel Pentium III, n/a, Microsoft Visual
  5395.      C/C++
  5396.         * The `OFLOWWATCH' option has no effect.
  5397.  
  5398.         * The `USEDEBUG' option has no effect.
  5399.  
  5400.         * The `USEMMAP' option has no effect.
  5401.  
  5402.         * No support for reading symbols.
  5403.  
  5404.         * The address of an illegal memory access cannot be determined.
  5405.  
  5406.         * The `-d' option to the `mpatrol' command has no effect.
  5407.  
  5408. F.1 Adding a new operating system
  5409. =================================
  5410.  
  5411.    * Add a new `TARGET' and/or `SYSTEM' definition in `target.h'.  The
  5412.      `TARGET' macro is for fundamentally different operating systems,
  5413.      whereas the `SYSTEM' macro is for differentiating variations of a
  5414.      particular operating system.
  5415.  
  5416.    * Make any necessary modifications to `config.h'.
  5417.  
  5418.    * Add any support for memory allocation in `memory.c'.
  5419.  
  5420.    * Add any support for stack traversal in `stack.c'.
  5421.  
  5422.    * Add any support for signals in `signals.c'.
  5423.  
  5424.    * Add any support for threads in `mutex.c'.
  5425.  
  5426.    * Add any support for filenames in `diag.c'.
  5427.  
  5428.    * Add a new version and date format (or use an existing one) in
  5429.      `version.c'.
  5430.  
  5431.    * Decide if the `malloc()' replacements should be used from
  5432.      `malloc.c'.
  5433.  
  5434.    * Add any support for invoking commands in `mpatrol.c'.
  5435.  
  5436.    * Add a new subdirectory in the `build' directory that contains a
  5437.      `Makefile' and any other files that are required to build the
  5438.      library on the new operating system.
  5439.  
  5440. F.2 Adding a new processor architecture
  5441. =======================================
  5442.  
  5443.    * Add a new `ARCH' definition in `target.h'.
  5444.  
  5445.    * Make any necessary modifications to `config.h'.
  5446.  
  5447.    * Add any support for memory allocation in `memory.c'.
  5448.  
  5449.    * Add any support for stack traversal in `stack.c'.
  5450.  
  5451. F.3 Adding a new object file format
  5452. ===================================
  5453.  
  5454.    * Add a new `FORMAT' definition in `target.h'.
  5455.  
  5456.    * Make any necessary modifications to `config.h'.
  5457.  
  5458.    * Add any support for stack traversal in `stack.c'.
  5459.  
  5460.    * Add any support for symbol reading in `symbol.c'.
  5461.  
  5462. Appendix G Notes
  5463. ****************
  5464.  
  5465.    This section contains information about known bugs and limitations
  5466. in the mpatrol library as well as listing potential future enhancements.
  5467.  
  5468.    Bugs should be reported to <mpatrol@cbmamiga.demon.co.uk> along with
  5469. the details of the operating system, processor architecture and object
  5470. file format that the mpatrol library is being used with -- and don't
  5471. forget to include the version of the mpatrol library you are using!
  5472. Keep in mind that I only have access to an Amiga running RedHat
  5473. Linux/m68k 5.1 and AmigaOS 3.1, so I will be most likely unable to
  5474. reproduce most of the system-specific bugs.  A bug report that comes
  5475. with an associated fix will be most welcome.
  5476.  
  5477.    Enhancement requests and source code containing enhancements should
  5478. also be sent to <mpatrol@cbmamiga.demon.co.uk> or the mpatrol
  5479. discussion group at `http://www.egroups.com/group/mpatrol/'.  If you
  5480. are planning to implement an enhancement, let me know first in case I
  5481. am (or someone else is) working towards the same goal -- that way, work
  5482. won't be wasted.  If you wish to send me source code changes please
  5483. send the changes as context diffs or in an e-mail attachment as a
  5484. compressed tar archive.
  5485.  
  5486. G.1 Notes for all platforms
  5487. ===========================
  5488.  
  5489.    * C++ support is still fairly limited, and will possibly only work
  5490.      for older C++ code due to the way the operators are overridden
  5491.      (i.e. there are no exceptions versions of the functions).  There
  5492.      are also likely to be potential problems with the macros which
  5493.      redefine `malloc()' and `operator new', etc., since there may be
  5494.      member functions in code that will mistakenly be redefined if their
  5495.      names match the macro definitions, and also means that calls to
  5496.      placement `new' will not work at all.  Also, explicit references to
  5497.      `operator new' rather than `new' are likely to result in
  5498.      compilation errors, and the way that source level information is
  5499.      obtained for `operator delete' means that the resulting code will
  5500.      not be thread-safe.
  5501.  
  5502.    * Need to add support for 64-bit processors.  This shouldn't be too
  5503.      hard, but I haven't got access to a 64-bit environment to test it,
  5504.      so I haven't bothered yet.
  5505.  
  5506.    * The thread-safe code in the library doesn't yet work properly,
  5507.      probably because of the recursion flag which is incremented or
  5508.      decremented before the mutex is locked.  Hence, the threads test
  5509.      (`tests/pass/test5.c') doesn't work yet.
  5510.  
  5511.    * Need to make the library re-entrant.  This could be achieved by
  5512.      moving the static variables in `memory.c', `stack.c', `mutex.c',
  5513.      `diag.c', `option.c' and `sbrk.c' into the `infohead' structure
  5514.      and then having an array of `infohead' structures from which to
  5515.      allocate new memory headers when a new one is required.  This is
  5516.      only necessary for Amiga shared libraries and Netware NLMs since
  5517.      UNIX and Windows platforms allocate a new copy of the data section
  5518.      in a shared library or DLL when it is opened by a new process.
  5519.  
  5520.    * The current implementation of call stack traversal is limited and
  5521.      will only likely work for unoptimised code.  A much better
  5522.      solution would be write the implementation at a lower level in
  5523.      assembly, but this is much less portable.  Perhaps there is a
  5524.      library which can be used to perform this across many operating
  5525.      systems and processor architectures, or maybe someone would like to
  5526.      write one?  I can think of many applications that would benefit
  5527.      from such a library besides this one(1).
  5528.  
  5529.    * An alternative implementation for call stack traversal uses the
  5530.      functions `__builtin_frame_address()' and
  5531.      `__builtin_return_address()' that are available when the library
  5532.      is compiled with `gcc'.  However, they can only traverse a number
  5533.      of stack frames at compile-time, not run-time so there is a
  5534.      maximum number of stack frames that can be traversed at any one
  5535.      time.  The implementation depends on both of these builtin
  5536.      functions returning `NULL' when the top of stack is reached.  If
  5537.      this is not the case then this method cannot be used or should
  5538.      only be used with a small number of fixed stack frames.
  5539.  
  5540.    * Add a function to display a stack trace of the current call stack
  5541.      to a file stream and another function to walk all current memory
  5542.      allocations invoking a call-back function for each one
  5543.      encountered.  Note that there is still an issue with call-back
  5544.      functions if they call mpatrol library functions, since this will
  5545.      lead to recursion.
  5546.  
  5547.    * In object file formats that support nested symbols (such as ELF),
  5548.      the current implementation will tend to show some shortcomings.
  5549.      This is because there is currently no nesting count in the
  5550.      function that deals with symbol name lookup, so the wrong symbol
  5551.      name may be displayed in diagnostics.
  5552.  
  5553.    * In object file formats that don't store the sizes of symbols (such
  5554.      as basic COFF, or when using the GNU BFD library), the current
  5555.      implementation will simply assume that the current symbol
  5556.      terminates at the beginning of the next symbol in the virtual
  5557.      address space.
  5558.  
  5559.    * There still appears to be a problem with the code address to line
  5560.      number mapping provided by the GNU BFD library, and this needs to
  5561.      be looked into further.  Also need to store filename and line
  5562.      number information in all call stacks so that the information can
  5563.      be used at program termination.  May also need to display this
  5564.      information in the `__mp_printinfo()' function and add this
  5565.      information to the profiling output file so that `mprof' can make
  5566.      use of it.
  5567.  
  5568.    * Perhaps add the ability to profile memory operations such as
  5569.      `memcpy()' and `memset()' to the existing memory allocation
  5570.      profiling facility.  Also, add options to `mprof' to write out
  5571.      files that can be used by graph drawing software for a better
  5572.      visualisation of the profiling information.  Finally, perhaps add
  5573.      an allocation call graph table to `mprof', similar to that
  5574.      produced by `gprof' for execution call graphs.
  5575.  
  5576.    * Perhaps add a memory usage profiling feature which would dump out
  5577.      memory usage statistics to a file at set intervals during run-time
  5578.      so that a table of memory usage could be built up.  This could
  5579.      also be extended to having a graphical tool which could display
  5580.      this information.
  5581.  
  5582.    * Add a CRC checksum to memory blocks and use it to check that freed
  5583.      memory allocations have not been corrupted when the `NOFREE' and
  5584.      `PRESERVE' options are in use on platforms which have no memory
  5585.      protection.  This could also be extended to marking allocated
  5586.      memory blocks and then displaying what blocks have changed after a
  5587.      certain period from within a debugger.  Another idea could be to
  5588.      display all memory allocations, etc.  made since a certain
  5589.      function was called from within a program.
  5590.  
  5591.    * Improve use of watch points by allowing an option which will only
  5592.      install write watch points instead of both read and write watch
  5593.      points.  Not only will this speed up the use of watch points, but
  5594.      will also cause less problems with reading from misaligned memory
  5595.      allocations.
  5596.  
  5597.    * Perhaps add memory protection to the simulated `sbrk' heap.
  5598.  
  5599.    * Add a `SHOWFREE' option to display a list of all free memory
  5600.      blocks at program termination for debugging purposes to view
  5601.      memory fragmentation.  If that option is added then perhaps
  5602.      `SHOWALL' should only be equivalent to `SHOWFREE', `SHOWFREED' and
  5603.      `SHOWUNFREED', and `SHOWMAP' and `SHOWSYMBOLS' should be
  5604.      explicitly given.
  5605.  
  5606.    * Add an option, similar to `NOFREE', that would prevent a freed
  5607.      memory allocation from being used until a certain number of memory
  5608.      allocations later.  This would be far less of a resource-hogger
  5609.      than the `NOFREE' option and might catch just as many errors but
  5610.      might be extremely hard to implement.
  5611.  
  5612.    * Add an extra piece of information in the log file summary which
  5613.      highlights the peak number of memory allocations in use at any one
  5614.      time during program execution.
  5615.  
  5616.    * Add versions of `mallopt()', `mallinfo()', `memorymap()',
  5617.      `mallocctl()', `mallocblksize()' and `msize()' which are provided
  5618.      in many other malloc libraries.  These won't necessarily behave in
  5619.      exactly the same way as existing implementations, but at least
  5620.      there won't be link errors when compiling source code which uses
  5621.      them.
  5622.  
  5623.    * Perhaps add debugging/tracing versions of the string manipulation
  5624.      functions, such as `strlen()' and `strcmp()' in much the same way
  5625.      as was done for the memory operation functions.  The only problem
  5626.      with this would be locale support, but perhaps it might be easier
  5627.      just to assume the C locale to begin with.  Also need to have
  5628.      better detection of internal and free blocks when displaying
  5629.      memory range errors.
  5630.  
  5631.    * Perhaps add definitions of `xmalloc()', `xrealloc()', etc. which
  5632.      never return `NULL' on failure, and perhaps also add definitions of
  5633.      `XtMalloc()', `XtRealloc()', etc. for X-Window programming.  Some
  5634.      other malloc libraries provide versions of these but perhaps they
  5635.      are not needed if they are implemented using `malloc()',
  5636.      `realloc()', etc.
  5637.  
  5638.    * Add another library which can be linked in instead of mpatrol and
  5639.      replaces all calls to `__mp_alloc()', etc., with the original
  5640.      calls to `malloc()' and related functions.  This would be very
  5641.      useful for quickly removing all mpatrol functionality for perhaps
  5642.      even a release build, and might be useful for implementing
  5643.      functions such as `memalign()' which don't exist on many systems.
  5644.  
  5645.    * Write a set of functions that are compatible with those
  5646.      implemented by Checker, the `gcc' run-time memory access checker.
  5647.      This would allow every memory access to be checked in object files
  5648.      compiled with `gcc', not just pointers into the heap, and would
  5649.      provide error checking as effective as source code
  5650.      instrumentation.  Could also make use of the `etext', `edata' and
  5651.      `end' pointers that are set at run-time on most UNIX systems.
  5652.  
  5653.    * Perhaps remove the automatic logging of memory operations from
  5654.      programs run through the `mpatrol' command, and instead add an
  5655.      option to do it explicitly.
  5656.  
  5657.    * Add an option to specify that all failed memory allocations should
  5658.      abort (or at least give a warning) instead of returning a `NULL'
  5659.      pointer.  Also, perhaps add an option to display the partial
  5660.      contents of freed and unfreed allocations in the mpatrol log file.
  5661.  
  5662.    * Perhaps use GNU autoconf to automatically work out values for
  5663.      `config.h' on the platform it is being built on, and also use
  5664.      automake, libtool and install when building and installing files.
  5665.  
  5666.    * The postscript version of the quick reference card seems to print
  5667.      at an unusual offset on some printers.  Also, the PDF version gets
  5668.      created with 1 inch margins rather than half inch margins which
  5669.      means that it is one inch too large in both dimensions.  Need to
  5670.      figure out what is happening in both cases.
  5671.  
  5672.    ---------- Footnotes ----------
  5673.  
  5674.    (1) The latest release of the GNU C library includes a `backtrace()'
  5675. function which fills in an array of return addresses, but this requires
  5676. the presence of the library and some features of GCC.
  5677.  
  5678. G.2 Notes for UNIX platforms
  5679. ============================
  5680.  
  5681.    * Need to add watch point area support for non-Solaris operating
  5682.      systems.  This may be a case of preventing all heap memory from
  5683.      being accessed and providing a signal handler that is called when
  5684.      a read from or write to such memory triggers a signal.  The
  5685.      handler could then determine if the address is in a watch point,
  5686.      and if it is not it could unprotect the memory and return.
  5687.  
  5688.    * Need to improve watch point facility in order to speed it up by an
  5689.      order of magnitudes.  This will most likely involve removing all
  5690.      watch points when entering the library and replacing them when
  5691.      returning to user code.
  5692.  
  5693.    * Need to add advanced signal information for operating systems that
  5694.      do not support the `siginfo()' system call.  This information is
  5695.      used by the signal handler that handles the `SIGSEGV' signal in
  5696.      order to provide useful information about where an illegal memory
  5697.      access occurred.  However, there is currently a problem in that
  5698.      the call stack displayed from within that handler is not
  5699.      necessarily accurate with respect to the function at the top of
  5700.      the stack.  Also, signal handlers shouldn't technically call I/O
  5701.      functions in case of additional signals being caught so this may
  5702.      need to be improved.
  5703.  
  5704.    * Need to add a portable way of initialising the thread-safe version
  5705.      of the library when it is compiled by a C compiler.  There is
  5706.      already a solution to this problem when it is compiled by a C++
  5707.      compiler, but it may only work if the final program is linked with
  5708.      that specific compiler.
  5709.  
  5710.    * Need to add support for call stack traversal for at least the
  5711.      Alpha, MIPS, PA/RISC, PowerPC and RS/6000 processor architectures.
  5712.      The current implementation of call stack traversal for the
  5713.      Motorola 88xx0 family is also a bit flaky and so should only be
  5714.      used when the library and program are built unoptimised.
  5715.  
  5716.    * Need to add support for obtaining the program name from the stack
  5717.      for at least the Alpha, Motorola 88xx0, non-IRIX MIPS, PA/RISC,
  5718.      PowerPC and RS/6000 processor architectures.  Also need to add
  5719.      support for reading the program symbols from a suitable file in
  5720.      `/proc' for other operating systems that support it.  If there is
  5721.      no support for either of these methods then the `PROGFILE' option
  5722.      can currently be used to specify the program name at run-time.
  5723.  
  5724.    * The library cannot currently read any symbols from shared objects
  5725.      that have been read via `dlopen()'.  Also, on IRIX platforms no
  5726.      symbols can currently be read from any shared libraries that were
  5727.      used by a program.  This is because SGI have a slightly different
  5728.      interface to their dynamic linker that I haven't been able to
  5729.      figure out yet.
  5730.  
  5731.    * There is a problem on later Linux releases where the `_DYNAMIC'
  5732.      symbol is defined in `elf.h', thus resulting in a conflicting
  5733.      definition when compiling `symbol.c'.
  5734.  
  5735.    * The `-d' option to the `mpatrol' command does not always work on
  5736.      systems whose dynamic linkers support the `LD_PRELOAD' or
  5737.      `_RLD_LIST' environment variables.  This is because the object
  5738.      file format access libraries do not exist in shared form on such
  5739.      systems.  There is also likely to be an issue when running with
  5740.      thread-safe libraries.
  5741.  
  5742. G.3 Notes for Amiga platforms
  5743. =============================
  5744.  
  5745.    * Perhaps add support for building mpatrol as an Amiga shared
  5746.      library.  I attempted to do this in a previous release of mpatrol,
  5747.      but it would have involved too many source changes to get working
  5748.      fully.  Perhaps it's not even worth implementing as the archive
  5749.      library works fine.
  5750.  
  5751.    * Need to add proper support for call stack traversal for both the
  5752.      Motorola 680x0 and PowerPC processor architectures.  When `gcc' is
  5753.      being used then up to two stack frames can be traversed, but this
  5754.      should really be extended without requiring
  5755.      `MP_BUILTINSTACK_SUPPORT'.  When SAS/C is being used then there is
  5756.      no support for call stack traversal.
  5757.  
  5758.    * Need to add proper support for reading symbols from Amiga
  5759.      executable files.  When `gcc' is being used then the BFD library
  5760.      routines will be called to determine the symbols from the
  5761.      executable file, but this will only work for objects compiled with
  5762.      `gcc'.  When SAS/C is being used then there is no support for
  5763.      reading symbols from executable files.  Also need to add support
  5764.      for reading symbols from any shared libraries that are required by
  5765.      the program.
  5766.  
  5767.    * Possibly make use of other software such as Enforcer, Mungwall or
  5768.      MuLib in order to provide some form of memory protection.  The
  5769.      features of SegTracker could also be put to good use so that the
  5770.      file and hunk location of entries on the call stack could be
  5771.      determined.
  5772.  
  5773.    * When using SAS/C it is currently not possible to override the
  5774.      definition of `malloc()', etc., without including the `mpatrol.h'
  5775.      header file first.  This is because the compiler startup code and
  5776.      libraries call `malloc()' before everything is set up, and so the
  5777.      library cannot properly initialise itself if the `malloc()' that
  5778.      the startup code finds is the `malloc()' in the mpatrol library.
  5779.      This restriction does not exist when using `gcc'.
  5780.  
  5781. G.4 Notes for Windows platforms
  5782. ===============================
  5783.  
  5784.    * Need to add watch point area support, possibly by using guard
  5785.      pages as a basis for an implementation.
  5786.  
  5787.    * Need to add support for reading symbols from Windows executable
  5788.      files.  Also need to add support for reading symbols from any DLLs
  5789.      that are required by the program.  This may be possible in a
  5790.      limited fashion by using the GNU BFD library, but may only work
  5791.      with code compiled with `gcc'.
  5792.  
  5793.    * There seems to be a problem when mixing the archive version of the
  5794.      mpatrol library and the Microsoft C run-time library DLL, and vice
  5795.      versa.  This needs to be looked into, but for the moment, don't
  5796.      mix them.
  5797.  
  5798. G.5 Notes for Netware platforms
  5799. ===============================
  5800.  
  5801.    * The library has not yet been built (let alone tested) on Netware
  5802.      platforms.  The names of the system functions that the library
  5803.      calls for Netware were obtained by looking at Novell's developer
  5804.      documentation, so they may not even compile correctly without
  5805.      modification.
  5806.  
  5807.    * Need to add support for building the mpatrol library as an NLM.
  5808.      This is not currently a high priority requirement as the archive
  5809.      library should suffice for most purposes.
  5810.  
  5811.    * Need to add way to determine when the base of the stack has been
  5812.      reached during call stack traversal, since on Netware every
  5813.      application is really a thread running under one large process.
  5814.  
  5815.    * Need to add support for reading symbols from Netware load modules.
  5816.      Also need to add support for reading symbols from any NLMs that
  5817.      are required by the program.  This may be possible in a limited
  5818.      fashion by using the GNU BFD library, but may only work with code
  5819.      compiled with `gcc'.
  5820.  
  5821.    * Need to investigate if it is safe (or even possible) to override
  5822.      the definitions of `malloc()', etc., without including the
  5823.      `mpatrol.h' header file first.  Currently, non-macro definitions
  5824.      for these functions have been disabled in the Netware version of
  5825.      the library in case they affect other NLMs that are currently
  5826.      running.
  5827.  
  5828. Appendix H Related software
  5829. ***************************
  5830.  
  5831.    A list of software which helps in debugging dynamic memory
  5832. allocation problems is given below(1).  They all provide some of the
  5833. features that mpatrol contains and you may wish to use one of them to
  5834. solve your problem if you have trouble using mpatrol.  I have only ever
  5835. used Dbmalloc and Electric Fence, so I can't vouch for any of the
  5836. others, although if you have any recommendations feel free to let me
  5837. know so I can add them to this list.  In particular, there seems to be
  5838. a shortage of such programs for Netware platforms.
  5839.  
  5840.    * APurify
  5841.     Author
  5842.           Samuel Devulder (<Samuel.Devulder@info.unicaen.fr>)
  5843.  
  5844.     License
  5845.           Free Software
  5846.  
  5847.     Platforms
  5848.           AmigaOS
  5849.  
  5850.     Location
  5851.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  5852.  
  5853.     Overview
  5854.           Instruments an assembler source file to insert code that
  5855.           checks all memory accesses.
  5856.  
  5857.    * BoundsChecker
  5858.     Author
  5859.           NuMega Corporation (<info@numega.com>)
  5860.  
  5861.     License
  5862.           Commercial Software
  5863.  
  5864.     Platforms
  5865.           MS-DOS, Windows
  5866.  
  5867.     Location
  5868.           `http://www.numega.com/'
  5869.  
  5870.     Overview
  5871.           Detects and diagnoses errors in static, stack and heap memory
  5872.           and in memory and resource leaks.
  5873.  
  5874.    * Ccmalloc
  5875.     Author
  5876.           Armin Biere (<armin@ira.uk.de>)
  5877.  
  5878.     License
  5879.           GNU General Public License
  5880.  
  5881.     Platforms
  5882.           Various UNIX
  5883.  
  5884.     Location
  5885.           `http://iseran.ira.uka.de/~armin/ccmalloc/'
  5886.  
  5887.     Overview
  5888.           Can interface with `gdb' to find memory leaks, multiple
  5889.           deallocations and memory corruptions in C or C++ programs.
  5890.  
  5891.    * Chaperon
  5892.     Author
  5893.           John Reiser (<jreiser@BitWagon.com>)
  5894.  
  5895.     License
  5896.           Commercial Software
  5897.  
  5898.     Platforms
  5899.           Linux
  5900.  
  5901.     Location
  5902.           `http://www.BitWagon.com/chaperon.html'
  5903.  
  5904.     Overview
  5905.           Runs existing Intel Linux binary application programs, but
  5906.           checks for and reports bad behaviour in accessing memory.
  5907.  
  5908.    * Checker
  5909.     Author
  5910.           Tristan Gingold (<bug-checker@gnu.org>)
  5911.  
  5912.     License
  5913.           GNU General Public License
  5914.  
  5915.     Platforms
  5916.           Various UNIX
  5917.  
  5918.     Location
  5919.           `http://www.gnu.org/'
  5920.  
  5921.     Overview
  5922.           Detects illegal memory accesses when reading from
  5923.           uninitialised memory, writing to freed memory or outside
  5924.           memory blocks.  Also contains a garbage collector for
  5925.           detecting memory leaks.
  5926.  
  5927.    * CSRI malloc
  5928.     Author
  5929.           Mark Moraes (<moraes@deshaw.com>)
  5930.  
  5931.     License
  5932.           Free Software
  5933.  
  5934.     Platforms
  5935.           Various UNIX
  5936.  
  5937.     Location
  5938.           `ftp://ftp.cs.toronto.edu/pub/moraes/malloc.tar.gz'
  5939.  
  5940.     Overview
  5941.           A library of dynamic memory allocation functions with limited
  5942.           debugging and profiling support and detection of memory
  5943.           leaks.  Also comes with a graphical tool to display a dynamic
  5944.           picture of the heap.
  5945.  
  5946.    * Dbmalloc
  5947.     Author
  5948.           Conor P. Cahill (<cpcahil@virtech.vti.com>)
  5949.  
  5950.     License
  5951.           Free Software
  5952.  
  5953.     Platforms
  5954.           Various UNIX
  5955.  
  5956.     Location
  5957.           `http://www.clark.net/pub/dickey/dbmalloc/dbmalloc.html'
  5958.  
  5959.     Overview
  5960.           Provides replacements for memory management library functions
  5961.           and provides a full set of debugging features which detect
  5962.           memory overruns and other types of misuse.
  5963.  
  5964.    * Debauch
  5965.     Author
  5966.           Jon A. Christopher (<jac8792@tamu.edu>)
  5967.  
  5968.     License
  5969.           GNU General Public License
  5970.  
  5971.     Platforms
  5972.           Linux
  5973.  
  5974.     Location
  5975.           `http://quorum.tamu.edu/jon/gnu/'
  5976.  
  5977.     Overview
  5978.           A memory allocation debugger for C which will detect memory
  5979.           leaks, corrupted memory, stores to freed memory and more.
  5980.  
  5981.    * Debug Heap
  5982.     Author
  5983.           IBM Corporation (<info@ibm.com>)
  5984.  
  5985.     License
  5986.           Commercial Software
  5987.  
  5988.     Platforms
  5989.           IBM AS/400
  5990.  
  5991.     Location
  5992.           `http://www.as400.ibm.com/developer/porting/heapexternal.html'
  5993.  
  5994.     Overview
  5995.           A heap debugging environment with stack traceback for IBM
  5996.           AS/400 servers.
  5997.  
  5998.    * Dmalloc
  5999.     Author
  6000.           Gray Watson (<gray@burger.letters.com>)
  6001.  
  6002.     License
  6003.           Free Software
  6004.  
  6005.     Platforms
  6006.           Various UNIX, MS-DOS, Windows
  6007.  
  6008.     Location
  6009.           `http://www.dmalloc.com/'
  6010.  
  6011.     Overview
  6012.           A drop-in replacement for the system's memory management
  6013.           routines, providing powerful debugging facilities
  6014.           configurable at run-time.  Formerly known as Malloc_Dbg.
  6015.  
  6016.    * Electric Fence
  6017.     Author
  6018.           Bruce Perens (<Bruce@Pixar.com>)
  6019.  
  6020.     License
  6021.           GNU General Public License
  6022.  
  6023.     Platforms
  6024.           Various UNIX
  6025.  
  6026.     Location
  6027.           `ftp://ftp.perens.com/pub/ElectricFence/'
  6028.  
  6029.     Overview
  6030.           Uses virtual memory hardware to protect dynamically allocated
  6031.           memory in order to detect illegal memory accesses.
  6032.  
  6033.    * Enforcer
  6034.     Author
  6035.           Michael Sinz (<Enforcer@sinz.org>)
  6036.  
  6037.     License
  6038.           Free Software
  6039.  
  6040.     Platforms
  6041.           AmigaOS
  6042.  
  6043.     Location
  6044.           `http://www.iam.com/amiga/enforcer.html'
  6045.  
  6046.     Overview
  6047.           Sets up MMU tables to watch for illegal accesses to memory,
  6048.           such as the low page and non-existent pages.
  6049.  
  6050.    * FDA (Free Debug Allocator)
  6051.     Author
  6052.           Thomas Helvey (<tomh@inxpress.net>)
  6053.  
  6054.     License
  6055.           GNU General Public License
  6056.  
  6057.     Platforms
  6058.           Linux, Windows
  6059.  
  6060.     Location
  6061.           `http://www.debian.org/Packages/unstable/devel/fda.html'
  6062.  
  6063.     Overview
  6064.           Provides routines that can be plugged in to replace
  6065.           `malloc()', `calloc()', `realloc()' and `free()'.
  6066.  
  6067.    * Fortify
  6068.     Author
  6069.           Simon Bullen (<sbullen@cybergraphic.com.au>)
  6070.  
  6071.     License
  6072.           Free Software
  6073.  
  6074.     Platforms
  6075.           AmigaOS
  6076.  
  6077.     Location
  6078.           `http://www.geocities.com/SiliconValley/Horizon/8596/fortify.html'
  6079.  
  6080.     Overview
  6081.           Provides a fortified shell for memory allocations, trapping
  6082.           memory leaks, writes beyond and before memory blocks and
  6083.           writes to freed memory.
  6084.  
  6085.    * GC (Garbage Collector)
  6086.     Author
  6087.           Hans-J. Boehm (<boehm@acm.org>)
  6088.  
  6089.     License
  6090.           Free Software
  6091.  
  6092.     Platforms
  6093.           Various UNIX, AmigaOS, MS-DOS, Windows, MacOS
  6094.  
  6095.     Location
  6096.           `http://www.hpl.hp.com/personal/Hans_Boehm/gc/'
  6097.  
  6098.     Overview
  6099.           A general-purpose, garbage-collecting storage allocator that
  6100.           is intended to be used as a plug-in replacement for
  6101.           `malloc()', but can also be used to detect memory leaks.
  6102.  
  6103.    * GlowCode
  6104.     Author
  6105.           Electric Software, Inc. (<info@glowcode.com>)
  6106.  
  6107.     License
  6108.           Commercial Software
  6109.  
  6110.     Platforms
  6111.           Windows
  6112.  
  6113.     Location
  6114.           `http://www.glowcode.com/'
  6115.  
  6116.     Overview
  6117.           Provides a profiler, call coverage tool and resource browser
  6118.           which can detail memory leaks.
  6119.  
  6120.    * Great Circle
  6121.     Author
  6122.           Geodesic Systems (<info@geodesic.com>)
  6123.  
  6124.     License
  6125.           Commercial Software
  6126.  
  6127.     Platforms
  6128.           Various UNIX, Windows
  6129.  
  6130.     Location
  6131.           `http://www.geodesic.com/'
  6132.  
  6133.     Overview
  6134.           Provides complete heap profiling, allowing programmers to see
  6135.           what parts of a program are using the most memory with
  6136.           symbolic stack tracing.
  6137.  
  6138.    * HeapAgent
  6139.     Author
  6140.           MicroQuill (<info@microquill.com>)
  6141.  
  6142.     License
  6143.           Commercial Software
  6144.  
  6145.     Platforms
  6146.           Windows
  6147.  
  6148.     Location
  6149.           `http://www.microquill.com/'
  6150.  
  6151.     Overview
  6152.           Instruments the heap to provide heap error detection without
  6153.           the need to recompile any source code.
  6154.  
  6155.    * Insure++
  6156.     Author
  6157.           ParaSoft (<info@parasoft.com>)
  6158.  
  6159.     License
  6160.           Commercial Software
  6161.  
  6162.     Platforms
  6163.           Various UNIX, Windows
  6164.  
  6165.     Location
  6166.           `http://www.parasoft.com/'
  6167.  
  6168.     Overview
  6169.           Uses Source Code Instrumentation and Runtime Pointer Tracking
  6170.           technologies to pinpoint memory corruption, memory leaks,
  6171.           operations on unrelated pointers and more.  The Inuse
  6172.           graphical memory usage display tool is also provided with this
  6173.           software.
  6174.  
  6175.    * JMalloc
  6176.     Author
  6177.           Jeff Dunlop
  6178.  
  6179.     License
  6180.           Free Software
  6181.  
  6182.     Platforms
  6183.           MS-DOS, Windows
  6184.  
  6185.     Location
  6186.           `http://www.snippets.org/'
  6187.  
  6188.     Overview
  6189.           Provides tracing and debugging for `malloc()' and `operator
  6190.           new'.
  6191.  
  6192.    * JProbe
  6193.     Author
  6194.           KL Group (<info@klgroup.com>)
  6195.  
  6196.     License
  6197.           Commercial Software
  6198.  
  6199.     Platforms
  6200.           Various UNIX, Windows
  6201.  
  6202.     Location
  6203.           `http://www.klgroup.com/'
  6204.  
  6205.     Overview
  6206.           Helps pinpoint memory leaks in Java applications by tracking
  6207.           which objects hold references to other objects, and allows
  6208.           visualisation of memory usage in real-time.
  6209.  
  6210.    * Leak
  6211.     Author
  6212.           Christopher Phillips (<pefv700@hermes.chpc.utexas.edu>)
  6213.  
  6214.     License
  6215.           Free Software
  6216.  
  6217.     Platforms
  6218.           Various UNIX
  6219.  
  6220.     Location
  6221.           `http://sources.isc.org/devel/memleak/leak.txt'
  6222.  
  6223.     Overview
  6224.           Logs all calls to `malloc()' and related functions to
  6225.           database files with the filename and line number, then
  6226.           attempts to validate reallocations and deallocations and
  6227.           detect memory leaks.
  6228.  
  6229.    * LeakTracer
  6230.     Author
  6231.           Erwin Andreasen (<erwin@andreasen.org>)
  6232.  
  6233.     License
  6234.           Free Software
  6235.  
  6236.     Platforms
  6237.           Various UNIX
  6238.  
  6239.     Location
  6240.           `http://www.andreasen.org/LeakTracer/'
  6241.  
  6242.     Overview
  6243.           Detects memory leaks in C++ programs by overriding `operator
  6244.           new' and `operator delete'.
  6245.  
  6246.    * Leaky
  6247.     Author
  6248.           Kipp Hickman (<kipp@netscape.com>)
  6249.  
  6250.     License
  6251.           Netscape Public License
  6252.  
  6253.     Platforms
  6254.           Linux
  6255.  
  6256.     Location
  6257.           `http://www.mozilla.org/unix/leaky.html'
  6258.  
  6259.     Overview
  6260.           A program which helps find memory leaks and helps debug
  6261.           reference count problems with xpcom objects.
  6262.  
  6263.    * Malloc Debug
  6264.     Author
  6265.           Brandon S. Allbery <allbery@ncoast.org>
  6266.  
  6267.     License
  6268.           Free Software
  6269.  
  6270.     Platforms
  6271.           Various UNIX
  6272.  
  6273.     Location
  6274.           `http://www.leo.org/pub/comp/usenet/comp.sources.misc/malloc-debug/'
  6275.  
  6276.     Overview
  6277.           A debugging malloc package with stack traceback capability.
  6278.  
  6279.    * Malloc Debug Library
  6280.     Author
  6281.           Rammi (<rammi@quincunx.escape.de>)
  6282.  
  6283.     License
  6284.           Free Software
  6285.  
  6286.     Platforms
  6287.           Various UNIX
  6288.  
  6289.     Location
  6290.           `http://www.escape.de/users/quincunx/rmdebug.html'
  6291.  
  6292.     Overview
  6293.           Implements wrappers for the normal heap handling functions.
  6294.  
  6295.    * MallocTrace
  6296.     Author
  6297.           Mark Brader (<msb@sq.sq.com>)
  6298.  
  6299.     License
  6300.           Free Software
  6301.  
  6302.     Platforms
  6303.           Various UNIX
  6304.  
  6305.     Location
  6306.           `ftp://ftp.uu.net/usenet/comp.sources.unix/volume18/malloc-trace.Z'
  6307.  
  6308.     Overview
  6309.           A malloc package with call stack tracebacks.
  6310.  
  6311.    * MCheck
  6312.     Author
  6313.           Ronald Veldema (<rveldema@cs.vu.nl>)
  6314.  
  6315.     License
  6316.           GNU General Public License
  6317.  
  6318.     Platforms
  6319.           Linux
  6320.  
  6321.     Location
  6322.           `http://www.cs.vu.nl/~rveldema/mcheck/mcheck.html'
  6323.  
  6324.     Overview
  6325.           A memory usage and malloc checker for C and C++.  Comes with
  6326.           a Java application for browsing the trace files produced.
  6327.  
  6328.    * MEM
  6329.     Author
  6330.           Walter Bright
  6331.  
  6332.     License
  6333.           Free Software
  6334.  
  6335.     Platforms
  6336.           MS-DOS
  6337.  
  6338.     Location
  6339.           `http://www.snippets.org/'
  6340.  
  6341.     Overview
  6342.           A set of functions for debugging pointer and memory
  6343.           allocation problems.
  6344.  
  6345.    * MemCheck
  6346.     Author
  6347.           Stratosware Corporation (<info@stratosware.com>)
  6348.  
  6349.     License
  6350.           Commercial Software
  6351.  
  6352.     Platforms
  6353.           Windows
  6354.  
  6355.     Location
  6356.           `http://www.stratosware.com/'
  6357.  
  6358.     Overview
  6359.           Detects various run-time errors related to operating system
  6360.           resources and provides information on memory leaks.
  6361.  
  6362.    * MemDebug
  6363.     Author
  6364.           Rene Schmit (<rene.schmit@crpht.lu>)
  6365.  
  6366.     License
  6367.           Free Software
  6368.  
  6369.     Platforms
  6370.           Various UNIX, MS-DOS, Windows, MacOS
  6371.  
  6372.     Location
  6373.           `ftp://ftp.crpht.lu/pub/sources/memdebug/'
  6374.  
  6375.     Overview
  6376.           Provides memory management error detection, memory usage
  6377.           error detection, memory usage profiling and error simulation.
  6378.  
  6379.    * MemLeak
  6380.     Author
  6381.           Keith Packard (<keithp@ncd.com>)
  6382.  
  6383.     License
  6384.           Free Software
  6385.  
  6386.     Platforms
  6387.           Various UNIX
  6388.  
  6389.     Location
  6390.           `ftp://ftp.x.org/pub/R6.4/xc/util/memleak/'
  6391.  
  6392.     Overview
  6393.           Replaces the C library allocation functions and provides
  6394.           extensive memory checking, locating lost memory, detecting
  6395.           free memory still in use and stores to free memory along with
  6396.           stack tracebacks.
  6397.  
  6398.    * Memory Advisor
  6399.     Author
  6400.           PLATINUM Technology (<info@platinum.com>)
  6401.  
  6402.     License
  6403.           Commercial Software
  6404.  
  6405.     Platforms
  6406.           Various UNIX
  6407.  
  6408.     Location
  6409.           `http://www.platinum.com/'
  6410.  
  6411.     Overview
  6412.           Disassembles an object module into system-independent
  6413.           assembler code, inserts error checking instructions, then
  6414.           re-assembles the code.  Can also replace existing malloc
  6415.           libraries in order to provide greater error checking.
  6416.           Formerly known as Sentinel.
  6417.  
  6418.    * Memory Sleuth
  6419.     Author
  6420.           TurboPower (<info@turbopower.com>)
  6421.  
  6422.     License
  6423.           Commercial Software
  6424.  
  6425.     Platforms
  6426.           Windows
  6427.  
  6428.     Location
  6429.           `http://www.turbopower.com/'
  6430.  
  6431.     Overview
  6432.           Quickly tracks down memory leaks and resource allocation
  6433.           errors with C++Builder and Delphi.
  6434.  
  6435.    * Memprof
  6436.     Author
  6437.           Owen Taylor (<otaylor@redhat.com>)
  6438.  
  6439.     License
  6440.           GNU General Public License
  6441.  
  6442.     Platforms
  6443.           Linux
  6444.  
  6445.     Location
  6446.           `http://people.redhat.com/otaylor/memprof/'
  6447.  
  6448.     Overview
  6449.           A tool for profiling memory usage and detecting memory leaks.
  6450.  
  6451.    * Memproof
  6452.     Author
  6453.           AutomatedQA (<info@totalqa.com>)
  6454.  
  6455.     License
  6456.           Free Software
  6457.  
  6458.     Platforms
  6459.           Windows
  6460.  
  6461.     Location
  6462.           `http://www.totalqa.com/'
  6463.  
  6464.     Overview
  6465.           A memory and resource leak debugger for Borland's family of
  6466.           Windows compilers.
  6467.  
  6468.    * MemWatch
  6469.     Author
  6470.           Johan Lindh (<johan@link-data.com>)
  6471.  
  6472.     License
  6473.           Free Software
  6474.  
  6475.     Platforms
  6476.           Various UNIX, Windows
  6477.  
  6478.     Location
  6479.           `http://www.link-data.com/'
  6480.  
  6481.     Overview
  6482.           A fault-tolerant memory leak and corruption detection tool.
  6483.  
  6484.    * MemWatch
  6485.     Author
  6486.           Doug Walker (<walker@unx.sas.com>)
  6487.  
  6488.     License
  6489.           Free Software
  6490.  
  6491.     Platforms
  6492.           AmigaOS
  6493.  
  6494.     Location
  6495.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  6496.  
  6497.     Overview
  6498.           Provides replacement memory allocation routines for adding
  6499.           lots of memory debugging features that you link into your
  6500.           program.
  6501.  
  6502.    * MM (Shared Memory Library)
  6503.     Author
  6504.           Ralf S. Engelschall (<rse@engelschall.com>)
  6505.  
  6506.     License
  6507.           Free Software
  6508.  
  6509.     Platforms
  6510.           Various UNIX, Windows
  6511.  
  6512.     Location
  6513.           `http://www.engelschall.com/sw/mm/'
  6514.  
  6515.     Overview
  6516.           Simplifies the usage (and can help debug) the use of shared
  6517.           memory between related processes.
  6518.  
  6519.    * Mmalloc
  6520.     Author
  6521.           Mike Haertel (<mike@ai.mit.edu>) and Fred Fish
  6522.           (<fnf@cygnus.com>)
  6523.  
  6524.     License
  6525.           GNU General Public License
  6526.  
  6527.     Platforms
  6528.           Various UNIX
  6529.  
  6530.     Location
  6531.           `http://www.gnu.org/'
  6532.  
  6533.     Overview
  6534.           Uses `mmap()' to allocate separate pools of memory which can
  6535.           be mapped onto files for later reuse.
  6536.  
  6537.    * MPR
  6538.     Author
  6539.           Taj Khattra (<taj.khattra@pobox.com>)
  6540.  
  6541.     License
  6542.           Free Software
  6543.  
  6544.     Platforms
  6545.           Linux
  6546.  
  6547.     Location
  6548.           `http://metalab.unc.edu/pub/Linux/devel/lang/c/mpr-2.0.tar.gz'
  6549.  
  6550.     Overview
  6551.           Attempts to find memory leaks in C/C++ programs by writing a
  6552.           log file during program execution, which can then be
  6553.           processed for obtaining further information.
  6554.  
  6555.    * Mprof
  6556.     Author
  6557.           Ben Zorn (<zorn@cs.colorado.edu>)
  6558.  
  6559.     License
  6560.           Free Software
  6561.  
  6562.     Platforms
  6563.           Various UNIX
  6564.  
  6565.     Location
  6566.           `ftp://gatekeeper.dec.com/pub/misc/mprof-3.0.tar.Z'
  6567.  
  6568.     Overview
  6569.           Profiles the dynamic memory allocation behaviour of programs
  6570.           by logging details for each function than makes a memory
  6571.           allocation, including call stack tracebacks.
  6572.  
  6573.    * MuForce
  6574.     Author
  6575.           Thomas Richter (<thor@einstein.math.tu-berlin.de>)
  6576.  
  6577.     License
  6578.           Free Software
  6579.  
  6580.     Platforms
  6581.           AmigaOS
  6582.  
  6583.     Location
  6584.           `http://www.math.tu-berlin.de/~thor/thor/index.html'
  6585.  
  6586.     Overview
  6587.           Uses the MMU to monitor the system for any writes to
  6588.           non-existent memory and reports them over the serial port or
  6589.           any other output stream.
  6590.  
  6591.    * MuGuardianAngel
  6592.     Author
  6593.           Thomas Richter (<thor@einstein.math.tu-berlin.de>)
  6594.  
  6595.     License
  6596.           Free Software
  6597.  
  6598.     Platforms
  6599.           AmigaOS
  6600.  
  6601.     Location
  6602.           `http://www.math.tu-berlin.de/~thor/thor/index.html'
  6603.  
  6604.     Overview
  6605.           An extension to the MuForce program which protects free
  6606.           memory and detects all illegal memory accesses.
  6607.  
  6608.    * MuLib
  6609.     Author
  6610.           Thomas Richter (<thor@einstein.math.tu-berlin.de>)
  6611.  
  6612.     License
  6613.           Free Software
  6614.  
  6615.     Platforms
  6616.           AmigaOS
  6617.  
  6618.     Location
  6619.           `http://www.math.tu-berlin.de/~thor/thor/index.html'
  6620.  
  6621.     Overview
  6622.           Provides access to the MMU in modern Amigas so that features
  6623.           such as virtual memory can be implemented.
  6624.  
  6625.    * Mungwall
  6626.     Author
  6627.           Commodore-Amiga, Inc. (<info@amiga.de>)
  6628.  
  6629.     License
  6630.           Free Software
  6631.  
  6632.     Platforms
  6633.           AmigaOS
  6634.  
  6635.     Location
  6636.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  6637.  
  6638.     Overview
  6639.           Patches the system to check for free memory corruption.
  6640.  
  6641.    * NJAMD (Not Just Another Malloc Debugger)
  6642.     Author
  6643.           Mike Perry (<mikepery@fscked.org>)
  6644.  
  6645.     License
  6646.           GNU General Public License
  6647.  
  6648.     Platforms
  6649.           Various UNIX
  6650.  
  6651.     Location
  6652.           `http://fscked.org/proj/njamd.shtml/'
  6653.  
  6654.     Overview
  6655.           Helps track down a wide range of memory allocation problems
  6656.           and is divided into a front end executable and a library back
  6657.           end.
  6658.  
  6659.    * Optimizeit
  6660.     Author
  6661.           Intuitive Systems, Inc. (<info@optimizeit.com>)
  6662.  
  6663.     License
  6664.           Commercial Software
  6665.  
  6666.     Platforms
  6667.           Various UNIX, Windows
  6668.  
  6669.     Location
  6670.           `http://www.optimizeit.com/'
  6671.  
  6672.     Overview
  6673.           Attempts to locate memory leaks and performance bottlenecks
  6674.           in Java programs.
  6675.  
  6676.    * Plumber
  6677.     Author
  6678.           Owen O'Malley (<omalley@ics.uci.edu>)
  6679.  
  6680.     License
  6681.           GNU General Public License
  6682.  
  6683.     Platforms
  6684.           Linux, Solaris, SunOS
  6685.  
  6686.     Location
  6687.           `http://www.ics.uci.edu/~softtest/plumber.html'
  6688.  
  6689.     Overview
  6690.           A tool that replaces the normal Ada and C/C++ dynamic memory
  6691.           allocation functions and detects unfreed memory blocks.
  6692.  
  6693.    * Purify
  6694.     Author
  6695.           Rational Software (<info@rational.com>)
  6696.  
  6697.     License
  6698.           Commercial Software
  6699.  
  6700.     Platforms
  6701.           Various UNIX, Windows
  6702.  
  6703.     Location
  6704.           `http://www.rational.com/'
  6705.  
  6706.     Overview
  6707.           Uses Object Code Insertion technology to provide run-time
  6708.           error checking and memory leak detection.
  6709.  
  6710.    * QC
  6711.     Author
  6712.           Onyx Technology (<sales@onyx-tech.com>)
  6713.  
  6714.     License
  6715.           Commercial Software
  6716.  
  6717.     Platforms
  6718.           MacOS
  6719.  
  6720.     Location
  6721.           `http://www.onyx-tech.com/'
  6722.  
  6723.     Overview
  6724.           Runs in the background as a control panel and detects various
  6725.           memory errors which can then be caught and run under a
  6726.           debugger.
  6727.  
  6728.    * TestCenter
  6729.     Author
  6730.           CenterLine Development Systems (<info@centerline.com>)
  6731.  
  6732.     License
  6733.           Commercial Software
  6734.  
  6735.     Platforms
  6736.           Various UNIX
  6737.  
  6738.     Location
  6739.           `http://www.centerline.com/'
  6740.  
  6741.     Overview
  6742.           Detects memory leaks, duplicate frees and illegal access
  6743.           errors including loads from uninitialised objects.
  6744.  
  6745.    * Third Degree
  6746.     Author
  6747.           Digital Equipment Corporation (<info@digital.com>)
  6748.  
  6749.     License
  6750.           Commercial Software
  6751.  
  6752.     Platforms
  6753.           Digital UNIX
  6754.  
  6755.     Location
  6756.           `http://www.digital.com/'
  6757.  
  6758.     Overview
  6759.           A tool that performs memory access checks and memory leak
  6760.           detection of C, C++ and Fortran programs at run-time.
  6761.           Applications are modified using ATOM to determine if any
  6762.           memory locations are accessed when not properly allocated or
  6763.           initialised.
  6764.  
  6765.    * Vmalloc
  6766.     Author
  6767.           Kiem-Phong Vo (<kpv@research.att.com>)
  6768.  
  6769.     License
  6770.           AT&T Source Code License
  6771.  
  6772.     Platforms
  6773.           Various UNIX, Windows
  6774.  
  6775.     Location
  6776.           `http://akpublic.research.att.com/sw/tools/vmalloc/'
  6777.  
  6778.     Overview
  6779.           A discipline and method library for dynamic memory
  6780.           allocation, with support for regions, debugging and profiling.
  6781.  
  6782.    * Wipeout
  6783.     Author
  6784.           Olaf Barthel (<olsen@sourcery.han.de>)
  6785.  
  6786.     License
  6787.           Free Software
  6788.  
  6789.     Platforms
  6790.           AmigaOS
  6791.  
  6792.     Location
  6793.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  6794.  
  6795.     Overview
  6796.           Runs in the background checking free memory for corruption.
  6797.  
  6798.    * YAMD (Yet Another Malloc Debugger)
  6799.     Author
  6800.           Nate Eldredge (<neldredge@hmc.edu>)
  6801.  
  6802.     License
  6803.           GNU General Public License
  6804.  
  6805.     Platforms
  6806.           Linux, DOS
  6807.  
  6808.     Location
  6809.           `http://www3.hmc.edu/~neldredge/yamd/'
  6810.  
  6811.     Overview
  6812.           A tool for finding bugs related to dynamic memory allocation
  6813.           in C and C++, and includes paging mechanisms to catch bugs
  6814.           immediately.
  6815.  
  6816.    * ZeroFault
  6817.     Author
  6818.           The Kernel Group (<info@zerofault.com>)
  6819.  
  6820.     License
  6821.           Commercial Software
  6822.  
  6823.     Platforms
  6824.           AIX UNIX
  6825.  
  6826.     Location
  6827.           `http://www.zerofault.com/'
  6828.  
  6829.     Overview
  6830.           Uses run-time emulator technology to provide run-time error
  6831.           checking and memory leak detection.
  6832.  
  6833.    However, before you try out any of the above software, there may
  6834. already be a malloc library with debugging support on your system that
  6835. might be suitable for solving your problem.  For example, on Solaris 7
  6836. the following libraries are available:
  6837.  
  6838. `malloc(3c)'
  6839.      Trade-off between performance and efficiency.
  6840.  
  6841. `malloc(3x)'
  6842.      Slower performance, space-efficient.
  6843.  
  6844. `bsdmalloc(3x)'
  6845.      Better performance, space-inefficient.
  6846.  
  6847. `mtmalloc(3t)'
  6848.      Thread-safe memory allocator.
  6849.  
  6850. `mapmalloc(3x)'
  6851.      Uses `mmap()' instead of `sbrk()' to allocate heap space.
  6852.  
  6853. `watchmalloc(3x)'
  6854.      Uses watch point areas to check for overflows.
  6855.  
  6856.    On platforms with the GNU C library, such as Linux, there are several
  6857. environment variables that can be used to enable various debugging
  6858. features of `malloc()', etc.  There are also extra functions provided
  6859. in the library which can be used to aid in debugging, and some shell
  6860. scripts which can translate return addresses or locate unfreed memory
  6861. allocations in the log files produced.  Useful information on the
  6862. debugging features available within the GNU C library is located at
  6863. `http://sdb.suse.de/sdb/en/html/aj_debug.html'.
  6864.  
  6865.    ---------- Footnotes ----------
  6866.  
  6867.    (1) This list can be considered to be a slightly more up to date
  6868. version of `Debugging Tools for Dynamic Storage Allocation and Memory
  6869. Management' (`http://www.cs.colorado.edu/~zorn/MallocDebug.html') by Ben
  6870. Zorn (<zorn@cs.colorado.edu>).
  6871.  
  6872. Function index
  6873. **************
  6874.  
  6875. __mp_check:                                        
  6876.           See ``Appendix A Functions''.
  6877. __mp_epilogue:                                     
  6878.           See ``Appendix A Functions''.
  6879. __mp_info:                                         
  6880.           See ``Appendix A Functions''.
  6881. __mp_memorymap:                                    
  6882.           See ``Appendix A Functions''.
  6883. __mp_nomemory:                                     
  6884.           See ``Appendix A Functions''.
  6885. __mp_printinfo:                                    
  6886.           See ``Appendix A Functions''.
  6887. __mp_prologue:                                     
  6888.           See ``Appendix A Functions''.
  6889. __mp_summary:                                      
  6890.           See ``Appendix A Functions''.
  6891. bcmp:                                              
  6892.           See ``Appendix A Functions''.
  6893. bcopy:                                             
  6894.           See ``Appendix A Functions''.
  6895. bzero:                                             
  6896.           See ``Appendix A Functions''.
  6897. calloc:                                            
  6898.           See ``Appendix A Functions''.
  6899. cfree:                                             
  6900.           See ``Appendix A Functions''.
  6901. expand:                                            
  6902.           See ``Appendix A Functions''.
  6903. free:                                              
  6904.           See ``Appendix A Functions''.
  6905. malloc:                                            
  6906.           See ``Appendix A Functions''.
  6907. memalign:                                          
  6908.           See ``Appendix A Functions''.
  6909. memccpy:                                           
  6910.           See ``Appendix A Functions''.
  6911. memchr:                                            
  6912.           See ``Appendix A Functions''.
  6913. memcmp:                                            
  6914.           See ``Appendix A Functions''.
  6915. memcpy:                                            
  6916.           See ``Appendix A Functions''.
  6917. memmem:                                            
  6918.           See ``Appendix A Functions''.
  6919. memmove:                                           
  6920.           See ``Appendix A Functions''.
  6921. memset:                                            
  6922.           See ``Appendix A Functions''.
  6923. operator delete:                                   
  6924.           See ``Appendix A Functions''.
  6925. operator delete[]:                                 
  6926.           See ``Appendix A Functions''.
  6927. operator new:                                      
  6928.           See ``Appendix A Functions''.
  6929. operator new[]:                                    
  6930.           See ``Appendix A Functions''.
  6931. pvalloc:                                           
  6932.           See ``Appendix A Functions''.
  6933. realloc:                                           
  6934.           See ``Appendix A Functions''.
  6935. recalloc:                                          
  6936.           See ``Appendix A Functions''.
  6937. set_new_handler:                                   
  6938.           See ``Appendix A Functions''.
  6939. strdup:                                            
  6940.           See ``Appendix A Functions''.
  6941. strndup:                                           
  6942.           See ``Appendix A Functions''.
  6943. strnsave:                                          
  6944.           See ``Appendix A Functions''.
  6945. strsave:                                           
  6946.           See ``Appendix A Functions''.
  6947. valloc:                                            
  6948.           See ``Appendix A Functions''.
  6949. Index
  6950. *****
  6951.  
  6952. -1:                                                
  6953.           See ``Appendix C Options''.
  6954. -2:                                                
  6955.           See ``Appendix C Options''.
  6956. -3:                                                
  6957.           See ``Appendix C Options''.
  6958. -A:                                                
  6959.           See ``Appendix C Options''.
  6960. -a:                                                
  6961.           See ``Appendix C Options''.
  6962. -C:                                                
  6963.           See ``Appendix C Options''.
  6964. -c:                                                
  6965.           See ``Appendix C Options''.
  6966. -d:                                                
  6967.           See ``Appendix C Options''.
  6968. -D:                                                
  6969.           See ``Appendix C Options''.
  6970. -e:                                                
  6971.           See ``Appendix C Options''.
  6972. -F:                                                
  6973.           See ``Appendix C Options''.
  6974. -f:                                                
  6975.           See ``Appendix C Options''.
  6976. -g:                                                
  6977.           See ``Appendix C Options''.
  6978. -G:                                                
  6979.           See ``Appendix C Options''.
  6980. -L:                                                
  6981.           See ``Appendix C Options''.
  6982. -l:                                                
  6983.           See ``Appendix C Options''.
  6984. -M:                                                
  6985.           See ``Appendix C Options''.
  6986. -m:                                                
  6987.           See ``Appendix C Options''.
  6988. -N:                                                
  6989.           See ``Appendix C Options''.
  6990. -n:                                                
  6991.           See ``Appendix C Options''.
  6992. -O:                                                
  6993.           See ``Appendix C Options''.
  6994. -o:                                                
  6995.           See ``Appendix C Options''.
  6996. -p:                                                
  6997.           See ``Appendix C Options''.
  6998. -P:                                                
  6999.           See ``Appendix C Options''.
  7000. -Q:                                                
  7001.           See ``Appendix C Options''.
  7002. -R:                                                
  7003.           See ``Appendix C Options''.
  7004. -S:                                                
  7005.           See ``Appendix C Options''.
  7006. -s:                                                
  7007.           See ``Appendix C Options''.
  7008. -U:                                                
  7009.           See ``Appendix C Options''.
  7010. -V:                                                
  7011.           See ``Appendix C Options''.
  7012. -v:                                                
  7013.           See ``Appendix C Options''.
  7014. -w:                                                
  7015.           See ``Appendix C Options''.
  7016. -x:                                                
  7017.           See ``Appendix C Options''.
  7018. -X:                                                
  7019.           See ``Appendix C Options''.
  7020. -Z:                                                
  7021.           See ``Appendix C Options''.
  7022. -z:                                                
  7023.           See ``Appendix C Options''.
  7024. .gdbinit:                                           See 7.5.
  7025. _RLD_LIST:                                          See 7.8.
  7026. ABI:                                                See 6.2.
  7027. acknowledgements:                                  
  7028.           See ``Foreword''.
  7029. adding a new object file format:                   
  7030.           See ``F.3 Adding a new object file format''.
  7031. adding a new operating system:                     
  7032.           See ``F.1 Adding a new operating system''.
  7033. adding a new processor architecture:               
  7034.           See ``F.2 Adding a new processor architecture''.
  7035. address space:                                      See 6.
  7036. address, physical:                                  See 6.1.
  7037. address, virtual:                                   See 6.1.
  7038. AIX, IBM RS/6000:                                  
  7039.           See ``Appendix F Supported systems''.
  7040. alignment:                                          See 7.1.
  7041. all (make target):                                  See 3.
  7042. alloca:                                             See 5.3.
  7043. allocated blocks:                                   See 11.1.
  7044. allocation algorithm:                               See 10.
  7045. allocation bin table:                               See 8.
  7046. allocation bins:                                    See 8.
  7047. allocation boundaries:                              See 8.
  7048. allocation byte:                                    See 7.3.
  7049. allocation index:                                   See 11.1.
  7050. allocation information:                            
  7051.           See ``Appendix A Functions''.
  7052. allocation type:                                    See 11.1.
  7053. ALLOCBYTE:                                         
  7054.           See ``Appendix B Environment''.
  7055. ALLOCSTOP:                                         
  7056.           See ``Appendix B Environment''.
  7057. ALLOWOFLOW:                                        
  7058.           See ``Appendix B Environment''.
  7059. amalloc:                                            See 9.
  7060. Amiga 4000/040:                                     See 3.
  7061. Amiga notes:                                       
  7062.           See ``G.3 Notes for Amiga platforms''.
  7063. AmigaOS, Motorola 680x0:                           
  7064.           See ``Appendix F Supported systems''.
  7065. ANSI:                                               See 9.
  7066. application binary interface:                       See 6.2.
  7067. APurify:                                           
  7068.           See ``Appendix H Related software''.
  7069. AR:                                                 See 3.
  7070. archive library:                                    See 2.
  7071. arenas:                                             See 9.
  7072. ATOM:                                              
  7073.           See ``Appendix H Related software''.
  7074. author, contacting:                                
  7075.           See ``Foreword''.
  7076. AutomatedQA:                                       
  7077.           See ``Appendix H Related software''.
  7078. AUTOSAVE:                                          
  7079.           See ``Appendix B Environment''.
  7080. BASIC:                                              See 5.
  7081. batch testing:                                      See 7.6.
  7082. best fit:                                           See 10.
  7083. BFD:                                                See 11.
  7084. bin:                                                See 8.
  7085. binary:                                            
  7086.           See ``Appendix B Environment''.
  7087. binary file:                                        See 8.
  7088. blocks:                                             See 11.1.
  7089. BoundsChecker:                                     
  7090.           See ``Appendix H Related software''.
  7091. breakpoint:                                         See 7.5.
  7092. bsdmalloc(3x):                                     
  7093.           See ``Appendix H Related software''.
  7094. BSS:                                                See 5.1.
  7095. buffers, overflow:                                  See 7.4.
  7096. bug reports:                                       
  7097.           See ``Foreword''.
  7098. bugs:                                              
  7099.           See ``Appendix G Notes''.
  7100. building the library:                               See 3.
  7101. bus errors:                                         See 7.1.
  7102. bytes compared:                                     See 11.1.
  7103. bytes copied:                                       See 11.1.
  7104. bytes located:                                      See 11.1.
  7105. bytes set:                                          See 11.1.
  7106. C:                                                  See 5.
  7107. C++:                                                See 5.
  7108. C++ mangled names:                                  See 11.1.
  7109. call sites:                                         See 8.
  7110. call stacks:                                        See 6.2.
  7111. call-by-value:                                      See 5.2.
  7112. callback functions:                                 See 11.1.
  7113. calling convention:                                 See 6.2.
  7114. CC:                                                 See 3.
  7115. Ccmalloc:                                          
  7116.           See ``Appendix H Related software''.
  7117. CenterLine Development Systems:                    
  7118.           See ``Appendix H Related software''.
  7119. CFLAGS:                                             See 3.
  7120. Chaperon:                                          
  7121.           See ``Appendix H Related software''.
  7122. CHECK:                                             
  7123.           See ``Appendix B Environment''.
  7124. CHECKALL:                                          
  7125.           See ``Appendix B Environment''.
  7126. CHECKALLOCS:                                       
  7127.           See ``Appendix B Environment''.
  7128. Checker:                                           
  7129.           See ``Appendix H Related software''.
  7130. CHECKFREES:                                        
  7131.           See ``Appendix B Environment''.
  7132. CHECKREALLOCS:                                     
  7133.           See ``Appendix B Environment''.
  7134. clean (make target):                                See 3.
  7135. clobber (make target):                              See 3.
  7136. COFF:                                               See 11.
  7137. command line options:                              
  7138.           See ``Appendix C Options''.
  7139. Commodore-Amiga, Inc.:                             
  7140.           See ``Appendix H Related software''.
  7141. common variables:                                   See 5.1.
  7142. compiler:                                           See 3.
  7143. compiling:                                          See 3.
  7144. contacting the author:                             
  7145.           See ``Foreword''.
  7146. contributors:                                      
  7147.           See ``Foreword''.
  7148. crash:                                              See 11.1.
  7149. CSRI malloc:                                       
  7150.           See ``Appendix H Related software''.
  7151. data sections:                                      See 5.1.
  7152. Dbmalloc:                                          
  7153.           See ``Appendix H Related software''.
  7154. Debauch:                                           
  7155.           See ``Appendix H Related software''.
  7156. Debug Heap:                                        
  7157.           See ``Appendix H Related software''.
  7158. debugger:                                           See 7.5.
  7159. debugging:                                          See 7.5.
  7160. debugging information:                              See 6.2.
  7161. decimal:                                           
  7162.           See ``Appendix B Environment''.
  7163. declarations, tentative:                            See 5.1.
  7164. DEFALIGN:                                          
  7165.           See ``Appendix B Environment''.
  7166. demangler:                                          See 11.1.
  7167. DG/UX, Intel 80x86:                                
  7168.           See ``Appendix F Supported systems''.
  7169. DG/UX, Motorola 88xx0:                             
  7170.           See ``Appendix F Supported systems''.
  7171. Digital Equipment Corporation:                     
  7172.           See ``Appendix H Related software''.
  7173. Digital UNIX:                                       See 9.
  7174. direct allocation table:                            See 8.
  7175. direct allocations:                                 See 8.
  7176. DLLs:                                               See 6.2.
  7177. Dmalloc:                                           
  7178.           See ``Appendix H Related software''.
  7179. documentation:                                      See 3.
  7180. dumping memory:                                     See 11.4.
  7181. dynamic link libraries:                             See 6.2.
  7182. dynamic linker:                                     See 6.2.
  7183. dynamic linking:                                    See 6.2.
  7184. dynamic memory allocations:                         See 5.3.
  7185. DYNIX/ptx, Intel 80x86:                            
  7186.           See ``Appendix F Supported systems''.
  7187. Electric Fence:                                    
  7188.           See ``Appendix H Related software''.
  7189. Electric Software, Inc.:                           
  7190.           See ``Appendix H Related software''.
  7191. ELF32:                                              See 11.
  7192. embedded libraries:                                 See 7.8.
  7193. embedded systems:                                   See 6.
  7194. Enforcer:                                          
  7195.           See ``Appendix H Related software''.
  7196. enhancements:                                      
  7197.           See ``Appendix G Notes''.
  7198. entry-point:                                        See 11.1.
  7199. environment:                                       
  7200.           See ``Appendix B Environment''.
  7201. epilogue function:                                  See 11.1.
  7202. error severity:                                     See 11.1.
  7203. errors, run-time:                                   See 1.
  7204. examples:                                           See 11.
  7205. executable files:                                   See 6.2.
  7206. FAILFREQ:                                          
  7207.           See ``Appendix B Environment''.
  7208. FAILSEED:                                          
  7209.           See ``Appendix B Environment''.
  7210. failure frequency:                                  See 7.6.
  7211. failure seed:                                       See 7.6.
  7212. FAQ:                                               
  7213.           See ``Foreword''.
  7214. fatal errors:                                       See 11.1.
  7215. fault, page:                                        See 6.1.
  7216. FDA (Free Debug Allocator):                        
  7217.           See ``Appendix H Related software''.
  7218. features:                                           See 2.
  7219. fence posts:                                        See 7.4.
  7220. file scope variables:                               See 5.1.
  7221. files, mapping:                                     See 6.1.
  7222. first fit:                                          See 10.
  7223. fitting allocations:                                See 11.3.
  7224. foreword:                                          
  7225.           See ``Foreword''.
  7226. Fortify:                                           
  7227.           See ``Appendix H Related software''.
  7228. FORTRAN:                                            See 5.
  7229. free blocks:                                        See 11.1.
  7230. free byte:                                          See 7.3.
  7231. free memory:                                        See 7.3.
  7232. FREEBYTE:                                          
  7233.           See ``Appendix B Environment''.
  7234. freed blocks:                                       See 11.1.
  7235. freed memory:                                       See 7.3.
  7236. FREESTOP:                                          
  7237.           See ``Appendix B Environment''.
  7238. FreshMeat:                                         
  7239.           See ``Foreword''.
  7240. function call stacks:                               See 6.2.
  7241. functions:                                         
  7242.           See ``Appendix A Functions''.
  7243. functions, callback:                                See 11.1.
  7244. functions, handler:                                 See 11.1.
  7245. future enhancements:                               
  7246.           See ``Appendix G Notes''.
  7247. g++:                                                See 11.1.
  7248. garbage collector:                                  See 5.3.
  7249. GC (Garbage Collector):                            
  7250.           See ``Appendix H Related software''.
  7251. gcc:                                                See 11.1.
  7252. gdb:                                                See 7.5.
  7253. general errors:                                     See 7.3.
  7254. Geodesic Systems:                                  
  7255.           See ``Appendix H Related software''.
  7256. getting updates:                                   
  7257.           See ``Foreword''.
  7258. GlowCode:                                          
  7259.           See ``Appendix H Related software''.
  7260. GNU C library:                                     
  7261.           See ``Appendix H Related software''.
  7262. Great Circle:                                      
  7263.           See ``Appendix H Related software''.
  7264. halting the library:                                See 7.5.
  7265. handler functions:                                  See 11.1.
  7266. heap:                                               See 5.3.
  7267. heap usage:                                         See 11.1.
  7268. HeapAgent:                                         
  7269.           See ``Appendix H Related software''.
  7270. HELP:                                              
  7271.           See ``Appendix B Environment''.
  7272. hexadecimal:                                       
  7273.           See ``Appendix B Environment''.
  7274. hidden memory:                                      See 10.
  7275. hints:                                              See 9.
  7276. HP/UX, HP PA/RISC:                                 
  7277.           See ``Appendix F Supported systems''.
  7278. IBM Corporation:                                   
  7279.           See ``Appendix H Related software''.
  7280. illegal memory accesses:                            See 11.3.
  7281. implementation details:                             See 10.
  7282. improving performance:                              See 9.
  7283. information about an allocation:                   
  7284.           See ``Appendix A Functions''.
  7285. installation:                                       See 3.
  7286. Insure++:                                          
  7287.           See ``Appendix H Related software''.
  7288. integration:                                        See 4.
  7289. internal blocks:                                    See 11.1.
  7290. Intuitive Systems, Inc.:                           
  7291.           See ``Appendix H Related software''.
  7292. Inuse:                                              See 3.
  7293. IRIX, MIPS:                                        
  7294.           See ``Appendix F Supported systems''.
  7295. JMalloc:                                           
  7296.           See ``Appendix H Related software''.
  7297. JProbe:                                            
  7298.           See ``Appendix H Related software''.
  7299. Kernel Group, The:                                 
  7300.           See ``Appendix H Related software''.
  7301. KL Group:                                          
  7302.           See ``Appendix H Related software''.
  7303. known bugs:                                        
  7304.           See ``Appendix G Notes''.
  7305. LARGEBOUND:                                        
  7306.           See ``Appendix B Environment''.
  7307. LaTeX:                                              See 3.
  7308. LD:                                                 See 3.
  7309. LD_PRELOAD:                                         See 7.8.
  7310. Leak:                                              
  7311.           See ``Appendix H Related software''.
  7312. LeakTracer:                                        
  7313.           See ``Appendix H Related software''.
  7314. Leaky:                                             
  7315.           See ``Appendix H Related software''.
  7316. library behaviour:                                  See 7.1.
  7317. library functions:                                  See 7.7.
  7318. library settings:                                   See 7.2.
  7319. library statistics:                                 See 7.2.
  7320. library, archive:                                   See 2.
  7321. library, building:                                  See 3.
  7322. library, mpatrol:                                   See 1.
  7323. library, shared:                                    See 2.
  7324. library, thread-safe:                               See 2.
  7325. LIMIT:                                             
  7326.           See ``Appendix B Environment''.
  7327. limitations:                                       
  7328.           See ``Appendix G Notes''.
  7329. limiting available memory:                          See 7.6.
  7330. line number table:                                  See 6.2.
  7331. linker:                                             See 3.
  7332. linking:                                            See 3.
  7333. links, symbolic:                                    See 3.
  7334. lint:                                               See 3.
  7335. lint (make target):                                 See 3.
  7336. Linux, Intel 80x86:                                
  7337.           See ``Appendix F Supported systems''.
  7338. Linux, Motorola 680x0:                             
  7339.           See ``Appendix F Supported systems''.
  7340. local static variables:                             See 5.1.
  7341. log file:                                           See 11.1.
  7342. LOGALL:                                            
  7343.           See ``Appendix B Environment''.
  7344. LOGALLOCS:                                         
  7345.           See ``Appendix B Environment''.
  7346. LOGFILE:                                           
  7347.           See ``Appendix B Environment''.
  7348. LOGFREES:                                          
  7349.           See ``Appendix B Environment''.
  7350. logging:                                            See 7.2.
  7351. LOGMEMORY:                                         
  7352.           See ``Appendix B Environment''.
  7353. LOGREALLOCS:                                       
  7354.           See ``Appendix B Environment''.
  7355. low memory handler function:                        See 11.1.
  7356. LynxOS, PowerPC:                                   
  7357.           See ``Appendix F Supported systems''.
  7358. make:                                               See 3.
  7359. Makefile:                                           See 3.
  7360. Malloc Debug:                                      
  7361.           See ``Appendix H Related software''.
  7362. Malloc Debug Library:                              
  7363.           See ``Appendix H Related software''.
  7364. malloc libraries for Solaris 7:                    
  7365.           See ``Appendix H Related software''.
  7366. malloc(3c):                                        
  7367.           See ``Appendix H Related software''.
  7368. malloc(3x):                                        
  7369.           See ``Appendix H Related software''.
  7370. Malloc_Dbg:                                        
  7371.           See ``Appendix H Related software''.
  7372. MallocTrace:                                       
  7373.           See ``Appendix H Related software''.
  7374. mangled names:                                      See 11.1.
  7375. manual layout:                                     
  7376.           See ``Foreword''.
  7377. manual pages:                                       See 3.
  7378. map of memory:                                      See 7.2.
  7379. mapmalloc(3x):                                     
  7380.           See ``Appendix H Related software''.
  7381. mapping files:                                      See 6.1.
  7382. MCheck:                                            
  7383.           See ``Appendix H Related software''.
  7384. MEDIUMBOUND:                                       
  7385.           See ``Appendix B Environment''.
  7386. MEM:                                               
  7387.           See ``Appendix H Related software''.
  7388. MemCheck:                                          
  7389.           See ``Appendix H Related software''.
  7390. MemDebug:                                          
  7391.           See ``Appendix H Related software''.
  7392. MemLeak:                                           
  7393.           See ``Appendix H Related software''.
  7394. Memory Advisor:                                    
  7395.           See ``Appendix H Related software''.
  7396. memory allocation profiling:                        See 8.
  7397. memory allocations:                                 See 5.
  7398. memory allocations, dynamic:                        See 5.3.
  7399. memory allocations, stack:                          See 5.2.
  7400. memory allocations, static:                         See 5.1.
  7401. memory blocks:                                      See 11.1.
  7402. memory dump:                                        See 11.4.
  7403. memory leak table:                                  See 8.
  7404. memory leaks:                                       See 11.7.
  7405. memory management interface:                        See 6.
  7406. memory management unit:                             See 6.1.
  7407. memory map:                                         See 7.2.
  7408. memory mapped files:                                See 6.1.
  7409. memory protection:                                  See 6.1.
  7410. Memory Sleuth:                                     
  7411.           See ``Appendix H Related software''.
  7412. memory usage:                                       See 11.1.
  7413. memory, physical:                                   See 6.1.
  7414. memory, virtual:                                    See 6.1.
  7415. Memprof:                                           
  7416.           See ``Appendix H Related software''.
  7417. Memproof:                                          
  7418.           See ``Appendix H Related software''.
  7419. MemWatch:                                          
  7420.           See ``Appendix H Related software''.
  7421. message passing:                                    See 6.3.
  7422. MicroQuill:                                        
  7423.           See ``Appendix H Related software''.
  7424. Microsoft:                                         
  7425.           See ``Appendix F Supported systems''.
  7426. misaligned data:                                    See 7.1.
  7427. misaligned memory accesses:                         See 6.1.
  7428. ML:                                                 See 5.3.
  7429. mleak:                                              See 7.8.
  7430. MM (Shared Memory Library):                        
  7431.           See ``Appendix H Related software''.
  7432. Mmalloc:                                           
  7433.           See ``Appendix H Related software''.
  7434. mmap:                                               See 7.1.
  7435. MMU:                                                See 6.1.
  7436. modules:                                            See 10.
  7437. MP_NOCPLUSPLUS:                                    
  7438.           See ``Appendix A Functions''.
  7439. mpatrol:                                            See 1.
  7440. mpatrol command:                                   
  7441.           See ``Appendix C Options''.
  7442. mpatrol features:                                   See 2.
  7443. mpatrol library:                                    See 1.
  7444. mpatrol.h:                                         
  7445.           See ``Appendix A Functions''.
  7446. mpatrol.log:                                        See 11.1.
  7447. MPATROL_OPTIONS:                                   
  7448.           See ``Appendix B Environment''.
  7449. MPATROL_VERSION:                                   
  7450.           See ``Appendix A Functions''.
  7451. MPR:                                               
  7452.           See ``Appendix H Related software''.
  7453. Mprof:                                             
  7454.           See ``Appendix H Related software''.
  7455. mprof:                                              See 8.
  7456. mtmalloc(3t):                                      
  7457.           See ``Appendix H Related software''.
  7458. MuForce:                                           
  7459.           See ``Appendix H Related software''.
  7460. MuGuardianAngel:                                   
  7461.           See ``Appendix H Related software''.
  7462. MuLib:                                             
  7463.           See ``Appendix H Related software''.
  7464. multi-processor systems:                            See 6.3.
  7465. Mungwall:                                          
  7466.           See ``Appendix H Related software''.
  7467. mutexes:                                            See 6.3.
  7468. NDEBUG:                                            
  7469.           See ``Appendix A Functions''.
  7470. Netware notes:                                     
  7471.           See ``G.5 Notes for Netware platforms''.
  7472. NJAMD (Not Just Another Malloc Debugger):          
  7473.           See ``Appendix H Related software''.
  7474. NOFREE:                                            
  7475.           See ``Appendix B Environment''.
  7476. non-static local variables:                         See 5.2.
  7477. NOPROTECT:                                         
  7478.           See ``Appendix B Environment''.
  7479. notes:                                             
  7480.           See ``Appendix G Notes''.
  7481. notes for all platforms:                           
  7482.           See ``G.1 Notes for all platforms''.
  7483. notes for Amiga platforms:                         
  7484.           See ``G.3 Notes for Amiga platforms''.
  7485. notes for Netware platforms:                       
  7486.           See ``G.5 Notes for Netware platforms''.
  7487. notes for UNIX platforms:                          
  7488.           See ``G.2 Notes for UNIX platforms''.
  7489. notes for Windows platforms:                       
  7490.           See ``G.4 Notes for Windows platforms''.
  7491. NuMega Corporation:                                
  7492.           See ``Appendix H Related software''.
  7493. object file formats, adding support:               
  7494.           See ``F.3 Adding a new object file format''.
  7495. object files:                                       See 6.2.
  7496. octal:                                             
  7497.           See ``Appendix B Environment''.
  7498. OFLAGS:                                             See 3.
  7499. OFLOWBYTE:                                         
  7500.           See ``Appendix B Environment''.
  7501. OFLOWSIZE:                                         
  7502.           See ``Appendix B Environment''.
  7503. OFLOWWATCH:                                        
  7504.           See ``Appendix B Environment''.
  7505. Onyx Technology:                                   
  7506.           See ``Appendix H Related software''.
  7507. operating systems:                                  See 6.
  7508. operating systems, adding support:                 
  7509.           See ``F.1 Adding a new operating system''.
  7510. optimisation:                                       See 3.
  7511. Optimizeit:                                        
  7512.           See ``Appendix H Related software''.
  7513. option summary:                                    
  7514.           See ``Appendix B Environment''.
  7515. options:                                           
  7516.           See ``Appendix C Options''.
  7517. original implementation:                            See 10.
  7518. other programs:                                    
  7519.           See ``Appendix H Related software''.
  7520. overflow buffers:                                   See 7.4.
  7521. overflow byte:                                      See 7.4.
  7522. overflow size:                                      See 7.4.
  7523. overview:                                           See 1.
  7524. overwrites:                                         See 7.4.
  7525. page:                                               See 6.1.
  7526. page fault:                                         See 6.1.
  7527. page size:                                          See 6.1.
  7528. PAGEALLOC:                                         
  7529.           See ``Appendix B Environment''.
  7530. parallel programming:                               See 6.3.
  7531. parameter variables:                                See 5.2.
  7532. Parasoft:                                          
  7533.           See ``Appendix H Related software''.
  7534. Pascal:                                             See 5.
  7535. peak memory usage:                                  See 11.1.
  7536. performance bottleneck:                             See 9.
  7537. performance improvements:                           See 9.
  7538. performance times:                                 
  7539.           See ``Appendix D Library performance''.
  7540. physical address:                                   See 6.1.
  7541. physical memory:                                    See 6.1.
  7542. platform-independent notes:                        
  7543.           See ``G.1 Notes for all platforms''.
  7544. platforms:                                         
  7545.           See ``Appendix F Supported systems''.
  7546. PLATINUM Technology:                               
  7547.           See ``Appendix H Related software''.
  7548. Plumber:                                           
  7549.           See ``Appendix H Related software''.
  7550. portability:                                        See 9.
  7551. POSIX threads:                                      See 6.3.
  7552. PRESERVE:                                          
  7553.           See ``Appendix B Environment''.
  7554. preserve freed contents:                            See 7.3.
  7555. prevent freeing memory:                             See 7.3.
  7556. printing:                                           See 3.
  7557. process id:                                        
  7558.           See ``Appendix C Options''.
  7559. processor architectures, adding support:           
  7560.           See ``F.2 Adding a new processor architecture''.
  7561. PROF:                                              
  7562.           See ``Appendix B Environment''.
  7563. PROFFILE:                                          
  7564.           See ``Appendix B Environment''.
  7565. profiling:                                          See 8.
  7566. profiling file format:                             
  7567.           See ``Appendix E Profiling file format''.
  7568. PROGFILE:                                          
  7569.           See ``Appendix B Environment''.
  7570. program counter:                                    See 6.2.
  7571. programs:                                          
  7572.           See ``Appendix H Related software''.
  7573. prologue function:                                  See 11.1.
  7574. Purify:                                            
  7575.           See ``Appendix H Related software''.
  7576. QC:                                                
  7577.           See ``Appendix H Related software''.
  7578. quick reference card:                               See 3.
  7579. random failures:                                    See 7.6.
  7580. Rational Software:                                 
  7581.           See ``Appendix H Related software''.
  7582. re-entrancy:                                        See 6.3.
  7583. read protection:                                    See 6.1.
  7584. REALLOCSTOP:                                       
  7585.           See ``Appendix B Environment''.
  7586. recompilation:                                      See 4.
  7587. recoverable errors:                                 See 11.1.
  7588. RedHat:                                            
  7589.           See ``Appendix F Supported systems''.
  7590. reference card:                                     See 3.
  7591. references:                                        
  7592.           See ``Foreword''.
  7593. registers:                                          See 5.2.
  7594. related software:                                  
  7595.           See ``Appendix H Related software''.
  7596. release builds:                                    
  7597.           See ``Foreword''.
  7598. reporting bugs:                                    
  7599.           See ``Foreword''.
  7600. return address:                                     See 6.2.
  7601. run-time errors:                                    See 1.
  7602. SAFESIGNALS:                                       
  7603.           See ``Appendix B Environment''.
  7604. sbrk:                                               See 7.1.
  7605. sections:                                           See 5.1.
  7606. semaphores:                                         See 6.3.
  7607. Sentinel:                                          
  7608.           See ``Appendix H Related software''.
  7609. settings:                                           See 7.2.
  7610. severity of errors:                                 See 11.1.
  7611. SFLAGS:                                             See 3.
  7612. shared libraries:                                   See 6.2.
  7613. shared library:                                     See 2.
  7614. shared memory:                                      See 6.3.
  7615. SHOWALL:                                           
  7616.           See ``Appendix B Environment''.
  7617. SHOWFREED:                                         
  7618.           See ``Appendix B Environment''.
  7619. SHOWMAP:                                           
  7620.           See ``Appendix B Environment''.
  7621. SHOWSYMBOLS:                                       
  7622.           See ``Appendix B Environment''.
  7623. SHOWUNFREED:                                       
  7624.           See ``Appendix B Environment''.
  7625. signal handler:                                     See 11.3.
  7626. signals:                                            See 2.
  7627. similar programs:                                  
  7628.           See ``Appendix H Related software''.
  7629. single-step:                                        See 7.5.
  7630. slot tables:                                        See 9.
  7631. SMALLBOUND:                                        
  7632.           See ``Appendix B Environment''.
  7633. software:                                          
  7634.           See ``Appendix H Related software''.
  7635. Solaris 7 malloc libraries:                        
  7636.           See ``Appendix H Related software''.
  7637. Solaris, Intel 80x86:                              
  7638.           See ``Appendix F Supported systems''.
  7639. Solaris, SPARC:                                    
  7640.           See ``Appendix F Supported systems''.
  7641. stack:                                              See 5.2.
  7642. stack memory allocations:                           See 5.2.
  7643. stack tracebacks:                                   See 6.2.
  7644. static memory allocations:                          See 5.1.
  7645. statistics:                                         See 7.2.
  7646. Stratosware Corporation:                           
  7647.           See ``Appendix H Related software''.
  7648. stress testing:                                     See 9.
  7649. stripped executable file:                           See 7.1.
  7650. summary of options:                                
  7651.           See ``Appendix B Environment''.
  7652. supported systems:                                 
  7653.           See ``Appendix F Supported systems''.
  7654. SVR4:                                               See 11.
  7655. swap file:                                          See 6.1.
  7656. swap in:                                            See 6.1.
  7657. swap out:                                           See 6.1.
  7658. swapping:                                           See 6.1.
  7659. symbol summary:                                     See 7.2.
  7660. symbol tables:                                      See 6.2.
  7661. symbolic links:                                     See 3.
  7662. symbols:                                            See 6.2.
  7663. system page size:                                   See 6.1.
  7664. systems:                                           
  7665.           See ``Appendix F Supported systems''.
  7666. systems, embedded:                                  See 6.
  7667. tentative declarations:                             See 5.1.
  7668. test suite:                                         See 2.
  7669. TestCenter:                                        
  7670.           See ``Appendix H Related software''.
  7671. testing:                                            See 7.6.
  7672. TeXinfo:                                            See 3.
  7673. TFLAGS:                                             See 3.
  7674. Third Degree:                                      
  7675.           See ``Appendix H Related software''.
  7676. thrashing:                                          See 6.1.
  7677. thread-safe library:                                See 2.
  7678. threads:                                            See 6.3.
  7679. threads library:                                    See 6.3.
  7680. times:                                             
  7681.           See ``Appendix D Library performance''.
  7682. tips:                                               See 9.
  7683. tracebacks:                                         See 6.2.
  7684. tracing:                                            See 7.2.
  7685. tree structure:                                     See 11.2.
  7686. TurboPower:                                        
  7687.           See ``Appendix H Related software''.
  7688. tutorial:                                           See 12.
  7689. type of allocation:                                 See 11.1.
  7690. underwrites:                                        See 7.4.
  7691. unfreed allocations:                                See 11.1.
  7692. UNFREEDABORT:                                      
  7693.           See ``Appendix B Environment''.
  7694. UNIX notes:                                        
  7695.           See ``G.2 Notes for UNIX platforms''.
  7696. updates:                                           
  7697.           See ``Foreword''.
  7698. USEDEBUG:                                          
  7699.           See ``Appendix B Environment''.
  7700. USEMMAP:                                           
  7701.           See ``Appendix B Environment''.
  7702. using mpatrol:                                      See 7.
  7703. using with a debugger:                              See 7.5.
  7704. utilities:                                          See 7.8.
  7705. variable length arrays:                             See 5.3.
  7706. variables, file scope:                              See 5.1.
  7707. variables, local static:                            See 5.1.
  7708. variables, non-static local:                        See 5.2.
  7709. variables, parameter:                               See 5.2.
  7710. VAX:                                                See 8.
  7711. virtual address:                                    See 6.1.
  7712. virtual memory:                                     See 6.1.
  7713. Vmalloc:                                           
  7714.           See ``Appendix H Related software''.
  7715. warranty:                                          
  7716.           See ``Foreword''.
  7717. watch points:                                       See 6.1.
  7718. watchmalloc(3x):                                   
  7719.           See ``Appendix H Related software''.
  7720. Windows notes:                                     
  7721.           See ``G.4 Notes for Windows platforms''.
  7722. Windows, Intel 80x86:                              
  7723.           See ``Appendix F Supported systems''.
  7724. Wipeout:                                           
  7725.           See ``Appendix H Related software''.
  7726. write protection:                                   See 6.1.
  7727. YAMD (Yet Another Malloc Debugger):                
  7728.           See ``Appendix H Related software''.
  7729. ZeroFault:                                         
  7730.           See ``Appendix H Related software''.
  7731.